C# 团结。如何将玩家转向运动方向

C# 团结。如何将玩家转向运动方向,c#,unity3d,game-engine,C#,Unity3d,Game Engine,正如您在本视频中所看到的,对象沿任何方向移动,但其模型不沿移动方向旋转。如何修复它 链接到视频 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共类控制器:单行为 { private CharacterController=null; 私有动画师Animator=null; 私人浮动速度=5f; void Start() { controller=gameObject.GetComponent(); animator=gameObje

正如您在本视频中所看到的,对象沿任何方向移动,但其模型不沿移动方向旋转。如何修复它

链接到视频

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类控制器:单行为
{
private CharacterController=null;
私有动画师Animator=null;
私人浮动速度=5f;
void Start()
{
controller=gameObject.GetComponent();
animator=gameObject.GetComponent();
}
无效更新()
{
float x=Input.GetAxis(“水平”);
float z=Input.GetAxis(“垂直”);
向量3移动=(transform.right*x)+(transform.forward*z);
控制器。移动(移动*速度*时间。延迟时间);
变量角度=数学Atan2(移动z,移动x)*数学Rad2Deg;
如果(x!=0 | | z!=0)animator.SetTrigger(“run”);
如果(x==0&&z==0)animator.SetTrigger(“空闲”);
}
}

不要使用
transform.forward
transform.right
创建移动向量,只需在世界空间中创建。然后,您可以将
transform.forward
设置为移动方向

此外,正如下面在评论中提到的,您应该

  • 避免使用完全相等来比较浮动。而是使用或使用您自己的阈值,如下所示

  • 避免每帧设置触发器。相反,您可以使用一个标志来确定您是否已处于空闲状态或已在运行状态,并且仅在您尚未执行操作时设置触发器

  • 使用系统集合;
    使用System.Collections.Generic;
    使用UnityEngine;
    公共类控制器:单行为
    {
    private CharacterController=null;
    私有动画师Animator=null;
    私人浮动速度=5f;
    布尔伊斯德尔;
    void Start()
    {
    controller=gameObject.GetComponent();
    animator=gameObject.GetComponent();
    isIdle=true;
    }
    无效更新()
    {
    float x=Input.GetAxis(“水平”);
    float z=Input.GetAxis(“垂直”);
    向量3移动=新向量3(x,0f,z);
    控制器。移动(移动*速度*时间。延迟时间);
    变量角度=数学Atan2(移动z,移动x)*数学Rad2Deg;
    如果(move.magnity>idleThreshold)
    {
    向前移动;
    如果(isIdle)
    {
    设置触发器(“运行”);
    isIdle=假;
    }
    } 
    否则如果(!isIdle)
    {
    设置触发器(“空闲”);
    isIdle=true;
    }
    }
    }
    
    不要使用
    transform.forward
    transform.right
    创建移动向量,只需在世界空间中创建。然后,您可以将
    transform.forward
    设置为移动方向

    此外,正如下面在评论中提到的,您应该

  • 避免使用完全相等来比较浮动。而是使用或使用您自己的阈值,如下所示

  • 避免每帧设置触发器。相反,您可以使用一个标志来确定您是否已处于空闲状态或已在运行状态,并且仅在您尚未执行操作时设置触发器

  • 使用系统集合;
    使用System.Collections.Generic;
    使用UnityEngine;
    公共类控制器:单行为
    {
    private CharacterController=null;
    私有动画师Animator=null;
    私人浮动速度=5f;
    布尔伊斯德尔;
    void Start()
    {
    controller=gameObject.GetComponent();
    animator=gameObject.GetComponent();
    isIdle=true;
    }
    无效更新()
    {
    float x=Input.GetAxis(“水平”);
    float z=Input.GetAxis(“垂直”);
    向量3移动=新向量3(x,0f,z);
    控制器。移动(移动*速度*时间。延迟时间);
    变量角度=数学Atan2(移动z,移动x)*数学Rad2Deg;
    如果(move.magnity>idleThreshold)
    {
    向前移动;
    如果(isIdle)
    {
    设置触发器(“运行”);
    isIdle=假;
    }
    } 
    否则如果(!isIdle)
    {
    设置触发器(“空闲”);
    isIdle=true;
    }
    }
    }
    
    没有代码,不可能知道你做了什么使它转向,或者不做以下操作:
    Vector3 move=new Vector3(x,0f,z)
    transform.forward=移动
    controller.Move(移动*速度*时间.deltaTime)一切都很好。你可以创建一个答案,我会接受它。没有代码,不可能知道你做了什么使它转向,或者没有这样做:
    Vector3 move=new Vector3(x,0f,z)
    transform.forward=移动
    controller.Move(移动*速度*时间.deltaTime)一切都很好。你可以创建一个答案,我会接受的。你仍然不应该直接比较
    float
    值,就像
    if(x!=0 | | z!=0)
    if(x==0&&z==0)
    。。。一般来说,如果你想在每一帧设置一个触发器。。。但除此之外,它应该是
    if(Mathf.approxiamtly(x,0)和&Mathf.approxiamtly(z,0)){animator.SetTrigger(“idle”);}或者{animator.SetTrigger(“run”)}
    或者直接
    animator.SetTrigger(Mathf.approxiamtly(x,0)和&Mathf.approxiamtly(z,0);“idle”:“run”)@derHugo啊,这就是我在不阅读所有内容的情况下复制和粘贴问题代码的结果。很好的建议,我在回答中包括了它们告诉我为什么旋转角度没有保存。当我把
    transform.forward=移动时,它总是被重置为零行进入空闲检查,并且还更改了空闲的确定方式您仍然不应该直接比较
    中的
    浮点值
    如果(x!=0 | | z!=0)
    如果(x==0&&z==0)
    。。。一般来说,如果要设置触发器,则有问题
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class MoveController : MonoBehaviour
    {
        private CharacterController controller = null;
        private Animator animator = null;
        private float speed = 5f;
    
    
        void Start()
        {
            controller = gameObject.GetComponent<CharacterController>();
            animator = gameObject.GetComponent<Animator>();
        }
    
        void Update()
        {
            float x = Input.GetAxis("Horizontal");
            float z = Input.GetAxis("Vertical");
    
            Vector3 move = (transform.right * x) + (transform.forward * z);
            controller.Move(move * speed * Time.deltaTime);
    
            var angle = Mathf.Atan2(move.z, move.x) * Mathf.Rad2Deg;
    
            if (x != 0 || z != 0) animator.SetTrigger("run");
            if (x == 0 && z == 0) animator.SetTrigger("idle");
        }
    }
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class MoveController : MonoBehaviour
    {
        private CharacterController controller = null;
        private Animator animator = null;
        private float speed = 5f;
        bool isIdle;
    
        void Start()
        {
            controller = gameObject.GetComponent<CharacterController>();
            animator = gameObject.GetComponent<Animator>();
            isIdle = true;
        }
    
        void Update()
        {
            float x = Input.GetAxis("Horizontal");
            float z = Input.GetAxis("Vertical");
    
            Vector3 move = new Vector3(x, 0f, z);
            controller.Move(move * speed * Time.deltaTime);
    
            var angle = Mathf.Atan2(move.z, move.x) * Mathf.Rad2Deg;
    
            if (move.magnitude > idleThreshold)
            {
                transform.forward = move;
                if (isIdle) 
                {
                    animator.SetTrigger("run");
                    isIdle = false;
                }
            } 
            else if (!isIdle)
            {
                animator.SetTrigger("idle");
                isIdle = true;
            }
        }
    }