Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Player Animation Rotating [SOLVED]

Discussion in 'Animation' started by cavalsilva, May 22, 2017.

  1. cavalsilva

    cavalsilva

    Joined:
    May 22, 2017
    Posts:
    3
    Hi everyone.

    I have a player with two animations, idle and walking, I control a transation with a bool variable, but when a transaction walk to idle, the walk animations is rotating.

    At below is coding, video and prints:

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (Input.GetMouseButtonDown(0))
    4.         {
    5.             RaycastHit hit;
    6.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    7.  
    8.             if (Physics.Raycast(ray, out hit))
    9.             {
    10.                 flag = true;
    11.                 endPoint = hit.point;
    12.                 endPoint.y = yAxis;
    13.                 Debug.Log(endPoint);
    14.  
    15.                 animator.SetBool ("Move", true);
    16.  
    17.             }
    18.  
    19.         }
    20.  
    21.         if (flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
    22.         {
    23.             gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 1 / (duration * (Vector3.Distance(gameObject.transform.position, endPoint))));
    24.  
    25.             //Faz com que o objeto olhe para onde foi clicado
    26.             transform.LookAt(endPoint);
    27.  
    28.         }
    29.         else if (flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
    30.         {
    31.             flag = false;
    32.             Debug.Log("Change Walk to Idle");
    33.             animator.SetBool ("Move", false);
    34.         }
    35.     }









    Thanks!
     
  2. cavalsilva

    cavalsilva

    Joined:
    May 22, 2017
    Posts:
    3
    I solved this change my script:

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (Input.GetMouseButtonDown(0))
    4.         {
    5.             RaycastHit hit;
    6.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    7.  
    8.             if (Physics.Raycast(ray, out hit))
    9.             {
    10.                 flag = true;
    11.                 endPoint = hit.point;
    12.                 endPoint.y = yAxis;
    13.                 Debug.Log(endPoint);
    14.  
    15.                 //Faz com que o objeto olhe para onde foi clicado
    16.                 // I CHANGED HERE!
    17.                 gameObject.transform.LookAt(endPoint);
    18.  
    19.                 animator.SetBool ("Move", true);
    20.  
    21.             }
    22.  
    23.             //Verifica se tocou nos hints
    24.             if (playerHit) {
    25.                 if (hit.collider.name == "HintRuaCantao") {
    26.                     countScore += 10;
    27.                     Destroy (Hints[0]);
    28.                 }
    29.                 if (hit.collider.name == "HintErosao") {
    30.                     countScore += 10;
    31.                     Destroy (Hints[1]);
    32.                 }
    33.                 if (hit.collider.name == "HintBueiro") {
    34.                     countScore += 10;
    35.                     Destroy (Hints[2]);
    36.                 }
    37.                 SetScoreText ();
    38.             }
    39.  
    40.         }
    41.  
    42.         if (flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
    43.         {
    44.             gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 1 / (duration * (Vector3.Distance(gameObject.transform.position, endPoint))));
    45.  
    46.         }
    47.         else if (flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
    48.         {
    49.             flag = false;
    50.             Debug.Log("Estou aqui");
    51.             animator.SetBool ("Move", false);
    52.         }
    53.     }