Unity运营商` - ='不能应用于`float'类型的操作数和`UnityEngine.Vector3'

时间:2014-10-17 13:18:00

标签: c# unity3d

我想让我的角色在Unity中跳跃,但是我收到了这个错误: 运算符-=' cannot be applied to operands of type float'和`UnityEngine.Vector3'

public class ExampleClass : MonoBehaviour {
    public float speed = 6.0F;
    public float jumpSpeed = 8.0F;
    public float gravity = 20.0F;
    private Vector3 moveDirection = Vector3.zero;
    void Update() {
        CharacterController controller = GetComponent<CharacterController>();
        if (controller.isGrounded) {
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;
            if (Input.GetButton("Jump"))
                moveDirection.y = jumpSpeed;

        }
        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move(moveDirection * Time.deltaTime);
    }
}

1 个答案:

答案 0 :(得分:1)

您无法在C#中一次修改单个轴的值。你必须重新分配整个载体:

moveDirection = new Vector3(moveDirection.x, moveDirection.y - (gravity * Time.deltaTime), moveDirection.z);

(从here“复制”......这是同一个问题!)