如何解决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。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。