1
votes

Okay, I am almost finished with my 2d rpg click to move game. 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 = Vector3.zero;
        if (movementDirection.sqrMagnitude > 0)
        {
            // Use >= to default to horizontal on both being equal
            if (movementDirection.x > movementDirection.y) 
                animDirection.x = 1;
            else
                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);
        }
    }
}

}

1

1 Answers

2
votes

I'm just starting out with Unity, but hope I can help.

From the video you've posted I've noticed that the sprite is only 'wrong' when the oldPosition and newPosition click differs in the X component, e.g. when you would click straight down it would show the desired behavior and sprite.

Have you tried printing out the x and y values that your code is using to calculate which sprite it's setting?

At first I thought that maybe it's because the values you set in the Blend Tree were at -1, 1 etc, and due to normalizing you sometimes wound up with 0.9 for a certain value.

Can you maybe try debugging it with the animator window open, like you did at the end? Writing down the values and comparing them between Desired and Undesired behavior might tell you something more.

Sorry I don't have a concrete solution to your problem, but I hope this helps.

Edit for clarification:
Basically, what I'm recommending is to:
1) Print out the values you are getting when the behavior happens, an example of how to print these things out is by using LogFormat, for example:

Debug.LogFormat("X: {0}, Y: {1}", xPosition, yPosition);

Now, you will get the values printed out when it finishes moving.

2) Write down the values for when the moving finishes with the 'Wrong' sprite, and keep clicking to move until the 'Right' sprite shows up. Write down the values again.

3) Compare each values, and find the differences. Now deduce why the differences are as they are, using the values in conjunction with your blend trees.

4) Once you know why, go back through your code/blend trees and rewrite/fix it to work as you intended it to.