变换不会向上或向下旋转

时间:2019-04-17 18:19:01

标签: c# unity3d

我目前正在制作一个动作脚本,可以左右旋转播放器,而上下旋转脖子。但是脖子不会向上或向下旋转。由于某种原因,如果我在游戏中更新脚本(例如,我只添加//test),它将起作用。知道为什么会这样,我该如何解决?

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour
{
    CharacterController characterController;

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

    public Transform neck;

    private Vector3 moveDirection = Vector3.zero;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        if (characterController.isGrounded)
        {
            //this
            // We are grounded, so recalculate
            // move direction directly from axes
            var mousePosX = Input.GetAxis("Mouse X");
            var mousePosY = Input.GetAxis("Mouse Y");

            transform.Rotate(0, mousePosX, 0);
            //Debug.Log("ABOVE - Y: " + mousePosY);
            Debug.Log("ABOVE - ROT: " + neck.rotation);
            neck.Rotate(Vector3.right, mousePosY, Space.Self);
            //Debug.Log("BELOW - Y: " + mousePosY);
            Debug.Log("BELOW - ROT: " + neck.rotation);

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

            moveDirection.z *= speed * 2;
            moveDirection.x *= speed;

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

        moveDirection.y -= gravity * Time.deltaTime;

        // Move the controller
        characterController.Move(moveDirection * Time.deltaTime);
    }
}

1 个答案:

答案 0 :(得分:0)

我发现了,我想我已经向播放器添加了一些动画,以便覆盖颈部转换,因此我无法通过脚本旋转它。我解决此问题的方法是制作一个LateUpdate函数,将旋转方向放在头部的Y轴上,这又覆盖了动画。