简体   繁体   English

3d Unity Player Object 同方向移动

[英]3d Unity Player Object moves in the same direction

I have a problem where the player moves in the direction but the animation of the charcter stays the same.我有一个问题,玩家朝某个方向移动,但角色的 animation 保持不变。 So if i click "w" key the player runs forward the animation works.因此,如果我单击“w”键,播放器将向前运行 animation 作品。 But when i click "s" for backwards the character does not rotate around to the direction it just moves/slides back with the character facing forward and no animation.但是当我向后单击“s”时,角色不会旋转到它只是移动/向后滑动的方向,角色面向前方并且没有 animation。 Please help!!请帮忙!!

public class Player : MonoBehaviour {

        private Animator anim;
        private CharacterController controller;

        public float speed = 600.0f;
        public float turnSpeed = 400.0f;
        private Vector3 moveDirection = Vector3.zero;
        public float gravity = 20.0f;
        private Vector3 curLoc;

    void Start () {
            controller = GetComponent <CharacterController>();
            anim = gameObject.GetComponentInChildren<Animator>();
        }

        void Update (){
            if (Input.GetKey ("w")) {
                anim.SetInteger ("AnimationPar", 1);
            }  else {
                anim.SetInteger ("AnimationPar", 0);
            }
        if (Input.GetKey("s"))
        {
            anim.SetInteger("Condition", 1);
        }
        else
        {
            anim.SetInteger("Condition", 0);
        }

        if (controller.isGrounded){
                moveDirection = transform.forward * Input.GetAxis("Vertical") * speed;
            }

            float turn = Input.GetAxis("Horizontal");
            transform.Rotate(0, turn * turnSpeed * Time.deltaTime, 0);
            controller.Move(moveDirection * Time.deltaTime);
            moveDirection.y -= gravity * Time.deltaTime;
        if (Input.GetKey(KeyCode.S))
        {

        }
        }
}

You need to show Animator, how animations are playing and transitioning.您需要向 Animator 展示动画是如何播放和过渡的。 Just open animator when game is playing and check if animation is playing where "Condition" parameter is passes.只需在游戏播放时打开动画器并检查 animation 是否正在播放“条件”参数传递的位置。 Check the animation transitions too.检查 animation 转换。 Also, I'd suggest you to change value "AnimationPar" to 0 when "s" is pressed.另外,我建议您在按下“s”时将值“AnimationPar”更改为 0。

 if (Input.GetKey("s"))
        {
            anim.SetInteger ("AnimationPar", 0);
            anim.SetInteger("Condition", 1);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM