Unity错误“无法通过实例引用访问成员'Vector3.right';相反,请使用类型名称对其进行限定”

问题描述

我最近一直在研究游戏开发,我想制作3D运动系统。我这样做是在遵循本教程https://youtu.be/KVOiKz-ddwk的某个时候完成的,他添加了forward.right = 0;。和right.y = 0;到移动功能,对他来说,它工作正常,但我收到错误“ Assets \ Scripts \ Movement.cs(37,9):错误CS0176:无法使用实例引用访问成员'Vector3.right';请使用输入名称代替”

显示完整代码

using UnityEngine;

public class Movement : MonoBehavIoUr
{
    [Serializefield] private float movementSpeed = 2f;
    private float currentspeed = 0f;
    private float speedSmoothVeLocity = 0f;
    private float speedSmoothTime = 0.1f;
    private float rotationSpeed = 0.1f;
    private float gravity = 3;

    private Transform mainCameraTransform = null;

    private CharacterController controller = null;
    private Animator animator = null;

    private void Start ()
    {
        controller = GetComponent<CharacterController>();
        animator = GetComponent<Animator>();

        mainCameraTransform = Camera.main.transform;
    }

    private void Update () 
    {
        Move();
    }

    private void Move ()
    {
        Vector2 movementInput = new Vector2(Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical"));

        Vector3 forward = mainCameraTransform.forward;
        Vector3 right = mainCameraTransform.right;

        forward.right = 0;
        right.y = 0;

        forward.normalize();
        right.normalize();

        Vector3 desiredMoveDirection = (forward * movementInput.y + right * movementInput.x).normalized;
        Vector3 gravityVector = Vector3.zero;
        
        if(!controller.isGrounded)
        {
            gravityVector.y -= gravity;
        }

        if(desiredMoveDirection != Vector3.zero)
        {
            transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(desiredMoveDirection),rotationSpeed);
            
        }
        
        float targetSpeed = movementSpeed * movementInput.magnitude;
        currentspeed = Mathf.Smoothdamp(currentspeed,targetSpeed,ref speedSmoothVeLocity,speedSmoothTime);

        controller.Move(desiredMoveDirection * currentspeed * Time.deltaTime);

        controller.Move(gravityVector * Time.deltaTime);

    }


}

我使用的是2020.1.1f1 Unity版本

解决方法

应该是 foward.y = 0;,而不是foward.right = 0;。我还找到了本教程在其中添加行的位置,这里是the link