Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(197评价)
价格: 4431.00元
Stealth人物旋转问题
谁家的猫发起了问答2017-06-05
5
回复
455
浏览

转的时候 还是会出现绕圈子的问题 输出了angle的值 绕圈子的时候不存在angle>180的情况 

用了官方的旋转代码 不知道为啥没有用....  

版本5.3.4f

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
{
    public float rotateSpeed = 15f;

    private float moveSpeed = 3;
    private float stopSpeed = 6;

    private Animator anim;

    void Awake()
    {
        anim = GetComponent<Animator>();
    }

    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        moveMentManagement(h, v);

    }

    private void moveMentManagement(float h,float v)
    {
        if (h!=0f || v!=0f)
        {
            Rotatings(h, v);
            float newSpeed = Mathf.Lerp(anim.GetFloat("Speed"), 5.6f, moveSpeed * Time.deltaTime);
            anim.SetFloat("Speed", newSpeed);
        }
        else
        {
            anim.SetFloat("Speed", 0);
        }
    }

    void Rotating(float h, float v)
    {
       
        Vector3 targetDir = new Vector3(h, 0f, v);
        
        Quaternion targetRotation =Quaternion.LookRotation(targetDir,Vector3.up);

        Quaternion newRotation = Quaternion.Lerp(GetComponent<Rigidbody>().rotation, targetRotation, Time.deltaTime*rotateSpeed);

        GetComponent<Rigidbody>().MoveRotation(newRotation);
        print(GetComponent<Rigidbody>().rotation);
        //Vector3 nowDir = transform.forward;
        //float angle = Vector3.Angle(targetDir, nowDir);
        //print(angle);

        ////if (angle > 180)
        ////{
        ////    print(h);
        ////    angle = 360 - angle;
        ////    angle = -angle;
        ////}

        //transform.Rotate(Vector3.up * angle * Time.deltaTime * rotateSpeed);

    }
    void Rotatings(float horizontal, float vertical)
    {
        // Create a new vector of the horizontal and vertical inputs.
        Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);

        // Create a rotation based on this new vector assuming that up is the global y axis.
        Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);

        // Create a rotation that is an increment closer to the target rotation from the player's rotation.
        Quaternion newRotation = Quaternion.Lerp(GetComponent<Rigidbody>().rotation, targetRotation, rotateSpeed * Time.deltaTime);

        // Change the players rotation to this new rotation.
        GetComponent<Rigidbody>().MoveRotation(newRotation);
    }
}

 

所有回复
发表回复
你还没有登录,请先 登录或 注册!