Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Unity 3D: Idle check direction

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 1.06k
    Answer it

    Basically I have created a 2d rpg click to move game on unity. My problem is that my player isn't facing the right direction once it has reached it's position, for example please take a look at this video: Unity problem 2 - YouTube[^]. Thank you. 

    This is my code: 

    private Animator anim;
    public float speed = 15f;
    private Vector3 target;
    private bool touched;
     
    
    void Start () {
        target = transform.position;
        anim = GetComponent<Animator> ();
    }
     
    void Update () {
        if (Input.GetMouseButtonDown(0)) {
            Vector3 mousePosition = Input.mousePosition;
            mousePosition.z = 10; // distance from the camera
            target = Camera.main.ScreenToWorldPoint(mousePosition);
            target.z = transform.position.z;
     
            var movementDirection = (target - transform.position).normalized;
     
            if (movementDirection.x != 0 || movementDirection.y != 0) {
                anim.SetBool("walking" , true);
                anim.SetFloat("SpeedX" , movementDirection.x);
                anim.SetFloat("SpeedY" , movementDirection.y);
     
                if (movementDirection.x < 0) {
                    anim.SetFloat("LastMoveX" , -1f);
                }
                else if (movementDirection.x > 0) {
                    anim.SetFloat("LastMoveX" , 1f);
                }
                else {
                    anim.SetFloat("LastMoveX" , 0f);
                }
                if (movementDirection.y > 0) {
                    anim.SetFloat("LastMoveY" , 1f);
                }
                else if (movementDirection.y < 0) {
                    anim.SetFloat("LastMoveY" , -1f);
                }
                else {
                    anim.SetFloat("LastMoveY" , 0f);
                }
            }
        }
        else {
            if (Mathf.Approximately(transform.position.x, target.x) && Mathf.Approximately(transform.position.y, target.y)) {
                touched = false;
                anim.SetBool("walking" , false);
            }
            else {
                transform.position = Vector3.MoveTowards(transform.position , target , speed * Time.deltaTime);
            }
        }
    }

     

 1 Answer(s)

  • Hi, The problem with this code is your animator not able to get correct values for LastMoveX and LastMoveY. As i can see in your video posted the value of LastMoveX =(1 or -1) and LastMoveY(1 or -1) for most of the times. whereas the animator values are set either in this format :

    see the format as in link: Format for animator value

    but according to your code the values are not coming correctly causing wrong animation Played.Try to run the following code to fix your Problem. By this your animator would get values in 0 and 1 format.Thanks

    public class moving : MonoBehaviour {
    
        private Vector3 target;
        float LastMoveX;
        float LastMoveY;
        string sideToMove;
        // Use this for initialization
        void Start () {
            target = this.transform.position;
        }
    
    void Update () {
        if (Input.GetMouseButtonDown (0)) 
            {
                Vector3 mousePosition = Input.mousePosition;
                mousePosition.z = 10; // distance from the camera
                target = Camera.main.ScreenToWorldPoint(mousePosition);
                target.z = transform.position.z;
    
                var movementDirection = (target - transform.position).normalized;
    
    
                if(Mathf.Abs(movementDirection.x) < Mathf.Abs(movementDirection.y))
                    sideToMove ="YAxis";
                else if(Mathf.Abs(movementDirection.x)> Mathf.Abs(movementDirection.y))
                    sideToMove ="XAxis";
                else
                    sideToMove ="default";
    
                switch(sideToMove)
                {
    
                case "XAxis":
                {
                    if (movementDirection.x < 0) {
                        anim.setLastMoveX= -1f;
                    }
                    else if (movementDirection.x > 0) {
                        LastMoveX= 1f;
                    }
                    else {
                        LastMoveX= 0f;
                    }
                    LastMoveY= 0f;
                }
                    break;
    
                case "YAxis":
                {
                    if (movementDirection.y > 0) {
                        LastMoveY= 1f;
                    }
                    else if (movementDirection.y < 0) {
                        LastMoveY= -1f;
                    }
                    else {
                        LastMoveY= 0f;
                    }
                    LastMoveX=0;
                }
                    break;
    
                dafault:
                    {
                        LastMoveX=0;
                        LastMoveY=0;
                    }
                }
                             anim.setFloat("LastMoveX",LastMoveX);
                             anim.setFloat("LastMoveY",LastMoveY);
    }
    }
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: