Click here to Skip to main content
15,844,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Okay, I am almost finished with my 2d rpg click to move game. I only have one problem, you see I'm having problems with my animation when it come to the Y-axis. Let me further explain. If you look at this video[^]you will be able to see that when I click forward my player, once it gets to it's position, face the wrong direction rather than facing straight towards the players. To make myself more clearer, this is an image[^] of the sprite sheet I am currently using, as you can see it has 8 directions. When you click here[^] (in the game), my player would walk down and face this direction[^] or this direction[^], rather than facing this direction[^] (the normal/preferred position). This also happens when I click here[^] (in the game) my player would walk up and face this direction[^] or face this direction[^], rather than facing this direction[^]. How can I make sure that my player face the right direction once it reached it's destination. Again this is only occurring within the Y-axis, so walking along the X-axis is fine.

    private Animator anim;
	public float speed = 15f;
	private Vector3 target;
	private bool touched;
	private bool playerMovementRef;

	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;
			Vector3 animDirection =;
			if (movementDirection.sqrMagnitude > 0)
				// Use >= to default to horizontal on both being equal
				if (movementDirection.x > movementDirection.y) 
					animDirection.x = 1;
					animDirection.y = 1;
				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);

What I have tried:

I tried using debug.log, but it wasn't giving me actual results that I can use.
I have also looked at this tutorial:, but it didn't really show me how to fix this bug!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900