Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 控制脚本不';当角色环顾四周时,不要改变方向_C#_Unity3d - Fatal编程技术网

C# 控制脚本不';当角色环顾四周时,不要改变方向

C# 控制脚本不';当角色环顾四周时,不要改变方向,c#,unity3d,C#,Unity3d,我正在使用官方Unity网站上的控制器脚本来移动我的角色。我还使用了一个脚本来使用鼠标转动相机。一切正常,直到角色环顾四周并面向不同的方向。然后WASD控件根据原始方向移动它们。例如,如果我旋转180度,W将使我向后移动,S将使我向前移动 我尝试使用transform.forward来解决这个问题,但我真的不知道我在做什么 运动脚本: CharacterController characterController; public float speed = 6.0f; public float

我正在使用官方Unity网站上的控制器脚本来移动我的角色。我还使用了一个脚本来使用鼠标转动相机。一切正常,直到角色环顾四周并面向不同的方向。然后WASD控件根据原始方向移动它们。例如,如果我旋转180度,W将使我向后移动,S将使我向前移动

我尝试使用transform.forward来解决这个问题,但我真的不知道我在做什么

运动脚本:

CharacterController characterController;

public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;

private Vector3 moveDirection = Vector3.zero;

void Start()
{
    characterController = GetComponent<CharacterController>();
}

void Update()
{
    if (characterController.isGrounded)
    {
        // We are grounded, so recalculate
        // move direction directly from axes

        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
        moveDirection *= speed;

        if (Input.GetButton("Jump"))
        {
            moveDirection.y = jumpSpeed;
        }
    }

    // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
    // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
    // as an acceleration (ms^-2)
    moveDirection.y -= gravity * Time.deltaTime;

    // Move the controller
    characterController.Move(moveDirection * Time.deltaTime);
}
CharacterController CharacterController;
公共浮子速度=6.0f;
公共浮子跳跃速度=8.0f;
公共浮子重力=20.0f;
专用矢量3移动方向=矢量3.0;
void Start()
{
characterController=GetComponent();
}
无效更新()
{
if(characterController.IsGround)
{
//我们停飞了,所以重新计算
//直接从轴移动方向
moveDirection=新矢量3(Input.GetAxis(“水平”),0.0f,Input.GetAxis(“垂直”);
移动方向*=速度;
if(Input.GetButton(“跳转”))
{
移动方向。y=跳跃速度;
}
}
//应用重力。重力乘以deltaTime两次(一次在这里,一次在下面)
//当移动方向乘以deltaTime时)。这是因为应应用重力
//作为加速(ms^-2)
moveDirection.y-=重力*时间增量;
//移动控制器
characterController.Move(moveDirection*Time.deltaTime);
}
感谢您的帮助:)

您可以使用将本地方向转换为世界方向。用您想要移动的本地方向调用它,它将返回相应的世界方向,您可以将该方向提供给CharacterController.move:

void Update()
{
    if (characterController.isGrounded)
    {
        // We are grounded, so recalculate
        // move direction directly from axes

        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection = moveDirection * speed;

        if (Input.GetButton("Jump"))
        {
            moveDirection.y = jumpSpeed;
        }
    }

    // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
    // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
    // as an acceleration (ms^-2)
    moveDirection.y -= gravity * Time.deltaTime;


    // Move the controller
    characterController.Move(worldMoveDirection * Time.deltaTime);
}
事实上,旧文档中使用的就是这种方法


这是因为
Move
使用世界空间坐标,但生成局部坐标

尝试改变

  moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));

  moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
  moveDirection = transform.right * Input.GetAxis("Horizontal") + transform.forward * Input.GetAxis("Vertical");