Unity:动画无效

时间:2018-01-11 10:57:16

标签: c# unity3d

我正在构建一个Android游戏,其中使用简单的游戏杆插件控制玩家。当我使用键盘控制播放器时,播放器动画可以正常工作,但是当我使用操纵杆ui按钮控制它时,即使播放器正在移动,步行动画也不会触发。以下是使用的代码。

玩家移动

using UnityEngine;
using UnityEngine.UI;

namespace CompleteProject
{
    public class PlayerMovement : MonoBehaviour
    {
        public float speed = 6f;            // The speed that the player will move at.
        public Text teleportText;
        public GameObject teleportCanvas;
        public GameObject teleOptionCanvas;
        Vector3 movement;                   // The vector to store the direction of the player's movement.
        Animator anim;                      // Reference to the animator component.
        Rigidbody playerRigidbody;          // Reference to the player's rigidbody.
        int floorMask;                      // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
        float camRayLength = 100f;          // The length of the ray from the camera into the scene.

        void Awake ()
        {
            // Create a layer mask for the floor layer.
            floorMask = LayerMask.GetMask ("Floor");

            // Set up references.
            anim = GetComponent <Animator> ();
            playerRigidbody = GetComponent <Rigidbody> ();
        }


        void FixedUpdate ()
        {
            // Store the input axes.
            float h = Input.GetAxisRaw ("Horizontal");
            float v = Input.GetAxisRaw ("Vertical");


            // Move the player around the scene.
            Move (h, v);

            // Turn the player to face the mouse cursor.
            Turning ();

            // Animate the player.
            Animating (h, v);
        }

        void Move (float h, float v)
        {
            // Set the movement vector based on the axis input.
            movement.Set (h, 0f, v);

            // Normalise the movement vector and make it proportional to the speed per second.
            movement = movement.normalized * speed * Time.deltaTime;

            // Move the player to it's current position plus the movement.
            playerRigidbody.MovePosition (transform.position + movement);
        }

        void Turning ()
        {
            // Create a ray from the mouse cursor on screen in the direction of the camera.
            Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);

            // Create a RaycastHit variable to store information about what was hit by the ray.
            RaycastHit floorHit;

            // Perform the raycast and if it hits something on the floor layer...
            if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
            {
                // Create a vector from the player to the point on the floor the raycast from the mouse hit.
                Vector3 playerToMouse = floorHit.point - transform.position;

                // Ensure the vector is entirely along the floor plane.
                playerToMouse.y = 0f;

                // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
                Quaternion newRotation = Quaternion.LookRotation (playerToMouse);

                // Set the player's rotation to this new rotation.
                playerRigidbody.MoveRotation (newRotation);
            }
        }

        void Animating (float h, float v)
        {
            // Create a boolean that is true if either of the input axes is non-zero.
            bool walking = h != 0f || v != 0f;

            // Tell the animator whether or not the player is walking.
            anim.SetBool ("IsWalking", walking);
        }

        void OnTriggerEnter(Collider col)
        {
            if (col.tag == "Teleport")
            {
                teleportCanvas.SetActive (true);
                if (ScoreManager.score < 200)
                    teleportText.text = "SORRY!! ATLEAST 200 POINTS REQUIRED";
                else if (ScoreManager.score >= 200) 
                {
                    teleportText.text = "READY TO GO!!";
                    teleOptionCanvas.SetActive (true);
                }

            }       
        }

        void OnTriggerStay(Collider col)
        {
            if(col.tag == "Teleport")
                Time.timeScale = 0.6f;
        }

        void OnTriggerExit(Collider col)
        { 
            if (col.tag == "Teleport") 
            {
                Time.timeScale = 1f;
                teleportCanvas.SetActive (false);
                teleOptionCanvas.SetActive (false);
            }
        }
}
}

操纵杆控制

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

namespace GeekGame.Input{
    public class JoystickMove : MonoBehaviour ,IDragHandler,IEndDragHandler
    {
        // remember turning is with joystick rotate
        // calculate the h and the v, then give it to PlayerMovement
        Animator anim;

        public static JoystickMove instance=null;

        public float _speed=6f;

        [Tooltip("the joystick radius ")]
        public float R=90f;

        private float _r;

        private Vector2 centerPos;

        private float _h;
        private float _v;

        public float H{
            get{return _h;}
        }
        public float V{
            get{return _v;}
        }

        void Awake(){

            if(instance!=null){
                Destroy(this.gameObject);
            }else{
                instance=this;
            }
        }
        void Start(){
            _r=1f*Screen.width/960f*R; //this to calculate the scale of screen
            centerPos=GetComponent<RectTransform>().position;

        }
        void SetHAndF(Vector2 pos){ //Horizontall and Vertical axes

            Vector2 diff=pos-centerPos;
            float distance=diff.magnitude;

            if(distance>_r){
                pos=centerPos+diff/distance*_r;

            }
            GetComponent<RectTransform>().position=pos;
            Vector2 move=pos-centerPos;
            _h=move.x;
            _v=move.y;
        }

        public void OnDrag(PointerEventData data)
        {   
            Vector2 newPos =new Vector2(data.position.x-20f,data.position.y-20f);
            //clamp the sprite
            SetHAndF(newPos);

        }

        public void OnEndDrag(PointerEventData data){
            Debug.Log("End Drag"+centerPos);
            GetComponent<RectTransform>().position=centerPos;
            SetHAndF(centerPos);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

通过在Joystickmovement中添加动画功能并在更新中调用它来修复。

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

namespace GeekGame.Input{
    public class JoystickMove : MonoBehaviour ,IDragHandler,IEndDragHandler
    {
        // remember turning is with joystick rotate
        // calculate the h and the v, then give it to PlayerMovement

        public static JoystickMove instance=null;
        public float _speed=6f;

        public GameObject player;

        public Animator animate;
        bool walking;

        [Tooltip("the joystick radius ")]
        public float R=90f;

        private float _r;

        private Vector2 centerPos;

        private float _h;
        private float _v;

        public float H{
            get{return _h;}
        }
        public float V{
            get{return _v;}
        }

        void Awake(){

            if(instance!=null){
                Destroy(this.gameObject);
            }else{
                instance=this;
            }

            animate = player.GetComponent<Animator> ();
        }
        void Start(){
            _r=1f*Screen.width/960f*R; //this to calculate the scale of screen
            centerPos=GetComponent<RectTransform>().position;

        }

        void Update()
        {
            Animating (_h, _v);         
        }
        void SetHAndF(Vector2 pos){ //Horizontall and Vertical axes

            Vector2 diff=pos-centerPos;
            float distance=diff.magnitude;

            if(distance>_r){
                pos=centerPos+diff/distance*_r;

            }
            GetComponent<RectTransform>().position=pos;
            Vector2 move=pos-centerPos;
            _h=move.x;
            _v=move.y;
        }

        public void OnDrag(PointerEventData data)
        {   
            Vector2 newPos =new Vector2(data.position.x-20f,data.position.y-20f);
            //clamp the sprite
            SetHAndF(newPos);

            Animating (_h, _v);

        }

        public void OnEndDrag(PointerEventData data){
            Debug.Log("End Drag"+centerPos);
            GetComponent<RectTransform>().position=centerPos;
            SetHAndF(centerPos);
        }

        void Animating (float h, float v)
        {
            // Create a boolean that is true if either of the input axes is non-zero.
            bool walking = h != 0f || v != 0f;

            // Tell the animator whether or not the player is walking.
            animate.SetBool ("IsWalking", walking);
        }
    }
}