-
Unity 3D: Idle check direction
over 8 years ago
-
over 8 years ago
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); } }
-
1 Answer(s)