Unity:尝试更改游戏对象的旋转时出现错误

时间:2018-05-01 13:30:11

标签: unity3d

所以我一直试图让我的角色移动和旋转,就像游戏中的常规方式一样。这是:以一定的速度向前移动,并能够转动角色的方向移动到其他方向。

我使用角色控制器,到目前为止,一切都已成功。但是,一旦我实际上将角色旋转到不同的方向,它就会向我发出错误。

错误:错误CS0029:无法隐式转换类型void' to UnityEngine.Vector3'

当我删除Vector3左行时,它再次起作用。所以我认为它与团结有关,不希望我使用transform.Rotate

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class basicmove : MonoBehaviour {


    public float walkSpeed;
    public float turnSpeed;


    void FixedUpdate() {
        CharacterController controller = GetComponent<CharacterController>();
        Vector3 forward = transform.TransformDirection(Vector3.forward);
        Vector3 left = transform.Rotate(Vector3.left*Time.deltaTime);

        if(Input.GetKey(KeyCode.W)){
            controller.SimpleMove(forward * walkSpeed);
        }
        if(Input.GetKey(KeyCode.A)){
            controller.SimpleMove(left * turnSpeed);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

要同时转动和移动,你可以做一些事情,最简单的是:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class basicmove : MonoBehaviour 
{
    public float walkSpeed;
    public float turnSpeed;
    private CharacterController controller;

    void Start() 
    {
        // Set here, so we don't have to constantly call getComponent.
        controller = getComponent<CharacterController>();
    }

    void FixedUpdate() 
    {
        if(controller != null) 
        {

            if(Input.GetKey(KeyCode.W))
            {
                // transform.forward is the forward direction of your object
                controller.SimpleMove(transform.forward * walkSpeed * Time.deltaTime);
            }

            if(Input.GetKey(KeyCode.A))
            {
                // transform.Rotate will rotate the transform using the information passed in.
                transform.Rotate(0, turnSpeed * Time.deltaTime, 0);
            }

            if(Input.GetKey(KeyCode.D))
            {
                // transform.Rotate will rotate the transform using the information passed in.
                transform.Rotate(0, -turnSpeed * Time.deltaTime, 0);
            }
        }
    }
}