My character moves in opposite directions

Hello!

The code below should move the character to the right and up. But the movement is being opposite. I already checked the objects orientation, in Unity, but I couldn’t resolve it. If I negate the two components of Vector3 - below - I can correct the movement, but that’s not how the instructor taught. Can someone help me please?

Move(new Vector3(-4, 0, -4));
public class Unit : MonoBehaviour

{
    private Vector3 targetPosition;
    [SerializeField] private float moveSpeed = 4f;
    private void Update()
    {
        Vector3 moveDirection = (transform.position - targetPosition).normalized;
        transform.position += moveDirection * moveSpeed * Time.deltaTime;
        if (Input.GetKeyDown(KeyCode.T))
        {
            Move(new Vector3(4, 0, 4));
        }
    }

    private void Move(Vector3 targetPosition)
    {
        this.targetPosition = targetPosition;
    }
}

did you reset the tranform of the gameobject in the inspector
transform.position += moveDirection + moveSpeed * Time.deltaTime;

1 Like

To flesh out what Willrun is asking, look at the position of the player when not playing
image
At this point, to look like the video, the character needs to be at 0,0,0

1 Like

Thank you both for your attention. The character’s rotation -Below- seems correct. The two last picture shows the transform of the two legs, which are different from the main component. I also tried to apply some possible solutions last night, but nothing worked. I stopped using You Tube to learn because it is full of bad videos that make us learn things the wrong way. Unity manual couldn’t help me too.

Forget about these legs. This is not something you want to change.

Your Unit script should be on the ‘Unit’ game object, not on the ‘Character_Dummy_Male_01’ game object.

You also set the direction from the target to you, which is the opposite of the direction you want to move in. You want to move from yourself to the target.

Vector3 moveDirection = (targetPosition - transform.position).normalized;

The format you want to use when determining the direction is
([where you want to go] - [where you are]).normalized

2 Likes

Damn! I looked a million times at the code and didn’t notice. But, it’s because I didn’t stop to think about the logic of the formula. I just tried to do the same as the instructor. Thanks @bixarrio.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms