为什么有时FPSController会像疯了似的不停地旋转呢?

时间:2018-07-04 01:55:58

标签: c# unity3d

它开始非常快地不停旋转360度。而且并非总是如此,但是一旦开始就不会停止。

我在层次结构中有一个FPSController,并附加了一个Rigidbody和Player Controller脚本。还有更多组件。 在FPSCamera上附加了“ Cam Mouse Look”脚本。

有时候,不停旋转是在x或y上,或在这两者上。

FPSController

发生旋转时,我看到FPSController是旋转的对象。但我不知道问题出在哪里。

“ Cam Mouse Look”脚本:

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

public class camMouseLook : MonoBehaviour
{
    Vector2 mouseLook;
    Vector2 smoothV;

    public float sensitivity = 5.0f;
    public float smoothing = 2.0f;

    GameObject character;

    // Use this for initialization
    void Start ()
    {
        character = this.transform.parent.gameObject;
    }

    // Update is called once per frame
    void Update ()
    {
        //if (Input.GetMouseButton(0))
        //{
            Cursor.visible = false;
            Cursor.lockState = CursorLockMode.Locked;

            var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));

            md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
            smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
            smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
            mouseLook += smoothV;

            mouseLook.y = Mathf.Clamp(mouseLook.y, -90f, 90f);

            transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
            character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
    }
}

播放器控制器脚本:

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

public class PlayerController : MonoBehaviour
{
    public float speed = 10.0f;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        float translatioin = Input.GetAxis("Vertical") * speed;
        float straffe = Input.GetAxis("Horizontal") * speed;
        translatioin *= Time.deltaTime;
        straffe *= Time.deltaTime;

        transform.Translate(straffe, 0, translatioin);

        if (Input.GetKeyDown("escape"))
            Cursor.lockState = CursorLockMode.None;
    }
}

0 个答案:

没有答案