Unity3D Mecanim 角色在转弯前停止



所以在过去的几天里,我一直在使用mecanim在Unity3D中开发角色控制器。它不是基于我自己的代码,而是基于我在网上找到的教程,当然该教程是为Unity 4准备的,所以我在这里和那里遇到了小问题,但直到现在我都无法解决。

所以基本的问题是,当我试图进行 180 度艰难的转弯时,我的角色似乎(无缘无故)停止了他所有的动力并慢慢转身,之后他又继续像往常一样奔跑,但我不明白为什么他会突然停止转弯。

这是我的字符逻辑脚本:

using UnityEngine;
using System.Collections;
public class characterLogic : MonoBehaviour {
[SerializeField]
private Animator animator;
[SerializeField]
private FollowCamera gamecam;
[SerializeField]
private float directionSpeed = 1.5f;
[SerializeField]
private float directionDampTime = 0.25f;
[SerializeField]
private float rotationDegreePerSecond = 120f;
[SerializeField]
private float speedDampTime = 0.05f;
private float speed = 0.0f;
private float direction = 0.0f;
private float charAngle = 0f;
private float horizontal = 0.0f;
private float vertical = 0.0f;
private AnimatorStateInfo stateInfo;
private AnimatorTransitionInfo transInfo;
private int m_LocomotionId = 0;
private int m_LocomotionPivotLId = 0;
private int m_LocomotionPivotRId = 0;
private int m_LocomotionPivotLTransId = 0;  
private int m_LocomotionPivotRTransId = 0;  
public Animator Animator
{
    get
    {
        return this.animator;
    }
}
public float Speed
{
    get
    {
        return this.speed;
    }
}
public float LocomotionThreshold { get { return 0.2f; } }

// Use this for initialization
void Start () {

    animator = GetComponent<Animator>();
    if(animator.layerCount >= 2)
    {
        animator.SetLayerWeight(1, 1);
    }   
    m_LocomotionId = Animator.StringToHash("Base Layer.Locomotion");
    m_LocomotionPivotLId = Animator.StringToHash("Base Layer.LocomotionPivotL");
    m_LocomotionPivotRId = Animator.StringToHash("Base Layer.LocomotionPivotR");
    m_LocomotionPivotLTransId = Animator.StringToHash("Base Layer.Locomotion -> Base Layer.LocomotionPivotL");
    m_LocomotionPivotRTransId = Animator.StringToHash("Base Layer.Locomotion -> Base Layer.LocomotionPivotR");
}
public void keysToWorldSpace (Transform root, Transform camera, ref float directionOut, ref float speedOut, ref float angleOut, bool isPivoting){
    Vector3 rootDirection = root.forward;
    Vector3 keyDirection = new Vector3 (horizontal, 0, vertical);
    speedOut = keyDirection.sqrMagnitude;
    //get camera rotation
    Vector3 cameraDirection = camera.forward;
    cameraDirection.y = 0.0f;
    Quaternion referentialShift = Quaternion.FromToRotation (Vector3.forward, cameraDirection);
    //convert key input to world space coordinates
    Vector3 moveDirection = referentialShift * keyDirection;
    Vector3 axisSign = Vector3.Cross (moveDirection, rootDirection);
    Debug.DrawRay (new Vector3(root.position.x, root.position.y + 2f, root.position.z), moveDirection, Color.green);
    Debug.DrawRay (new Vector3(root.position.x, root.position.y + 2f, root.position.z), axisSign, Color.red);
    Debug.DrawRay (new Vector3(root.position.x, root.position.y + 2f, root.position.z), rootDirection, Color.magenta);
    Debug.DrawRay (new Vector3(root.position.x, root.position.y + 2f, root.position.z), keyDirection, Color.blue);
    float angleRootToMove = Vector3.Angle(rootDirection, moveDirection) * (axisSign.y >= 0 ? -1f : 1f);
    if (!isPivoting)
    {
        angleOut = angleRootToMove;
    }
    angleRootToMove /= 180f;
    directionOut = angleRootToMove * directionSpeed;
}
// Update is called once per frame
void Update () {
    if (animator) {
        stateInfo = animator.GetCurrentAnimatorStateInfo(0);
        transInfo = animator.GetAnimatorTransitionInfo(0);
        horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");
        charAngle = 0f;
        direction = 0f;
        keysToWorldSpace (this.transform, gamecam.transform, ref direction, ref speed, ref charAngle, isInPivot());
        animator.SetFloat ("Speed", speed);
        animator.SetFloat ("Direction", direction, directionDampTime, Time.deltaTime);
        if(speed > LocomotionThreshold){
            if(!isInPivot()){
                Animator.SetFloat("Angle", charAngle);
            }
        }
        if(speed < LocomotionThreshold && Mathf.Abs(horizontal) < 0.05f){
            animator.SetFloat("Direction", 0f);
            animator.SetFloat("Speed", speed, speedDampTime, Time.deltaTime);
        }
        Debug.Log(Speed);
        Debug.Log(charAngle);
    }
}
void FixedUpdate() {
    if (IsInLocomotion () && ((direction >= 0 && horizontal >= 0) || (direction < 0 && horizontal < 0))) {
        Vector3 rotationAmount = Vector3.Lerp(Vector3.zero, new Vector3(0f, rotationDegreePerSecond * (horizontal < 0f ? -1f : 1f), 0f), Mathf.Abs(horizontal));
        Quaternion deltaRotation = Quaternion.Euler(rotationAmount * Time.deltaTime);
        this.transform.rotation = (this.transform.rotation * deltaRotation);
    }
}
public bool isInPivot(){
    return stateInfo.fullPathHash == m_LocomotionPivotLId || 
            stateInfo.fullPathHash == m_LocomotionPivotRId || 
            transInfo.nameHash == m_LocomotionPivotLTransId || 
            transInfo.nameHash == m_LocomotionPivotRTransId;
}
public bool IsInLocomotion(){
    return stateInfo.fullPathHash == m_LocomotionId;
}

}

我相信它要么与这个脚本有关,要么与 mecanim 中的过渡有关。我还将教程的成品(在此处找到:https://github.com/jm991/UnityThirdPersonTutorial)移植到Unity 5,并且在那里没有遇到同样的问题,我不完全确定给我带来此问题的区别是什么,但是如果你们中的任何人知道或发现,请告诉我。

我自己已经发现了问题!

这是新代码,以防有人对未来感兴趣:

using UnityEngine;
using System.Collections;
public class characterLogic : MonoBehaviour {
[SerializeField]
private Animator animator;
[SerializeField]
private FollowCamera gamecam;
[SerializeField]
private float directionSpeed = 1.5f;
[SerializeField]
private float directionDampTime = 0.25f;
[SerializeField]
private float rotationDegreePerSecond = 120f;
[SerializeField]
private float speedDampTime = 0.05f;
[SerializeField]
private float fovDampTime = 3f;
private float horizontal = 0.0f;
private float vertical = 0.0f;
private float speed = 0.0f;
private float direction = 0.0f;
private float charAngle = 0f;
private const float SPRINT_SPEED = 2.0f;
private const float SPRINT_FOV = 75.0f;
private const float NORMAL_FOV = 60.0f;
private const float WALK_SPEED = 0.1f;
private AnimatorStateInfo stateInfo;
private AnimatorTransitionInfo transInfo;
private int m_LocomotionId = 0;
private int m_LocomotionPivotLId = 0;
private int m_LocomotionPivotRId = 0;
private int m_LocomotionPivotLTransId = 0;  
private int m_LocomotionPivotRTransId = 0;  
public Animator Animator
{
    get
    {
        return this.animator;
    }
}
public float Speed
{
    get
    {
        return this.speed;
    }
}
public float LocomotionThreshold { get { return 0.2f; } }

// Use this for initialization
void Start () {

    animator = GetComponent<Animator>();
    if(animator.layerCount >= 2)
    {
        animator.SetLayerWeight(1, 1);
    }   
    m_LocomotionId = Animator.StringToHash("Base Layer.Locomotion");
    m_LocomotionPivotLId = Animator.StringToHash("Base Layer.LocomotionPivotL");
    m_LocomotionPivotRId = Animator.StringToHash("Base Layer.LocomotionPivotR");
    m_LocomotionPivotLTransId = Animator.StringToHash("Base Layer.Locomotion -> Base Layer.LocomotionPivotL");
    m_LocomotionPivotRTransId = Animator.StringToHash("Base Layer.Locomotion -> Base Layer.LocomotionPivotR");
}
public void keysToWorldSpace (Transform root, Transform camera, ref float directionOut, ref float speedOut, ref float angleOut, bool isPivoting){
    Vector3 rootDirection = root.forward;
    Vector3 keyDirection = new Vector3 (horizontal, 0, vertical);
    speedOut = keyDirection.sqrMagnitude;
    //get camera rotation
    Vector3 cameraDirection = camera.forward;
    cameraDirection.y = 0.0f;
    Quaternion referentialShift = Quaternion.FromToRotation(Vector3.forward, Vector3.Normalize(cameraDirection));
    //convert key input to world space coordinates
    Vector3 moveDirection = referentialShift * keyDirection;
    Vector3 axisSign = Vector3.Cross (moveDirection, rootDirection);
    Debug.DrawRay (new Vector3(root.position.x, root.position.y + 2f, root.position.z), moveDirection, Color.green);
    Debug.DrawRay (new Vector3(root.position.x, root.position.y + 2f, root.position.z), axisSign, Color.red);
    Debug.DrawRay (new Vector3(root.position.x, root.position.y + 2f, root.position.z), rootDirection, Color.magenta);
    Debug.DrawRay (new Vector3(root.position.x, root.position.y + 2f, root.position.z), keyDirection, Color.blue);
    float angleRootToMove = Vector3.Angle(rootDirection, moveDirection) * (axisSign.y >= 0 ? -1f : 1f);
    if (!isPivoting)
    {
        angleOut = angleRootToMove;
    }
    angleRootToMove /= 180f;
    directionOut = angleRootToMove * directionSpeed;
}
// Update is called once per frame
void Update () {
    if (animator) {
        stateInfo = animator.GetCurrentAnimatorStateInfo(0);
        transInfo = animator.GetAnimatorTransitionInfo(0);
        horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");
        charAngle = 0f;
        direction = 0f;
        float charSpeed = 0f;
        keysToWorldSpace (this.transform, gamecam.transform, ref direction, ref charSpeed, ref charAngle, isInPivot());
        if (Input.GetButton("Sprint"))
        {
            speed = Mathf.Lerp(speed, SPRINT_SPEED, Time.deltaTime);
            gamecam.GetComponent<Camera>().fieldOfView = Mathf.Lerp(gamecam.GetComponent<Camera>().fieldOfView, SPRINT_FOV, fovDampTime * Time.deltaTime);
        }
        else
        {
            speed = charSpeed;
            gamecam.GetComponent<Camera>().fieldOfView = Mathf.Lerp(gamecam.GetComponent<Camera>().fieldOfView, NORMAL_FOV, fovDampTime * Time.deltaTime);      
        }
        if (Input.GetButton("Walk"))
        {
            speed = Mathf.Lerp(speed, WALK_SPEED, Time.deltaTime);
        }
        else
        {
            speed = charSpeed;      
        }
        animator.SetFloat("Speed", speed, speedDampTime, Time.deltaTime);
        animator.SetFloat("Direction", direction, directionDampTime, Time.deltaTime);
        if(speed > LocomotionThreshold){
            if(!isInPivot()){
                Animator.SetFloat("Angle", charAngle);
            }
        }
        if(speed < LocomotionThreshold && Mathf.Abs(horizontal) < 0.05f){
            animator.SetFloat("Direction", 0f);
            animator.SetFloat("Speed", speed, speedDampTime, Time.deltaTime);
        }
        Debug.Log(Speed);
        Debug.Log(charAngle);
    }
}
void FixedUpdate() {
    if (IsInLocomotion () && ((direction >= 0 && horizontal >= 0) || (direction < 0 && horizontal < 0))) {
        Vector3 rotationAmount = Vector3.Lerp(Vector3.zero, new Vector3(0f, rotationDegreePerSecond * (horizontal < 0f ? -1f : 1f), 0f), Mathf.Abs(horizontal));
        Quaternion deltaRotation = Quaternion.Euler(rotationAmount * Time.deltaTime);
        this.transform.rotation = (this.transform.rotation * deltaRotation);
    }
}
public bool isInPivot(){
    return stateInfo.fullPathHash == m_LocomotionPivotLId || 
            stateInfo.fullPathHash == m_LocomotionPivotRId || 
            transInfo.nameHash == m_LocomotionPivotLTransId || 
            transInfo.nameHash == m_LocomotionPivotRTransId;
}
public bool IsInLocomotion(){
    return stateInfo.fullPathHash == m_LocomotionId;
}

}

事实证明,制作原始代码的家伙正在对他的速度使用潮湿的时间,因此如果他松开一个按钮,他不会立即静止不动。这是他还没有在他的教程中解释过的东西,所以我一定错过了。无论如何,我希望这可以帮助将来遇到类似问题的任何人。

最新更新