Can't seem to move in a straight line towards target

Hi,
I think it’s very simple but I have tried everything I can think of and I can’t get the desired behaviour. I am trying to move an object (enemy) towards another object (player) in a straight line. My understanding is the result of subtracting the current transform position from the target’s transform position yields the direction in which to move. So normalizing this direction vector and adding it to the enemies current position each frame should move towards the player. I have tried this, as well as MoveTowards and using both the Vector3.Translate method and reassigning the transform.position variable. Every time they move completely the wrong direction. I will attach a screenshot of what happens.

Here is my code on the enemy for moving, I have tried all the commented methods too, with the same result.

using UnityEngine;

public class EnemyMovement : MonoBehaviour
{
    public GameObject player;
    public float speed = 1.0f;


    // Update is called once per frame
    void Update()
    {  
        Vector3 direction = player.transform.position - transform.position;
        transform.position += direction.normalized * speed * Time.deltaTime;
        // transform.Translate(direction.normalized * speed * Time.deltaTime, Space.World);
        // Vector3 NewPosition = Vector3.MoveTowards(transform.position, player.transform.position, speed*Time.deltaTime);
        // transform.position = NewPosition;
    }
}

Thanks for any help!

That code works as you wanted (I just made a simple scene with two Cube GOs and cut/paste your code onto the enemy cube), are you sure you have given GameObject player the correct target object?

Thanks for the reply, I am as sure as I can be but I am an absolute unity beginner. I dragged the prefab player object onto the Player field (on this script in the inspector). If this is correct could there be some other setting I have changed or something? I’ve gone through the process a couple of times now and always the same result.

In the scene, the enemies clearly follow a straight line to a point, but that point is completely wrong, and there are no other GameObjects at that location. In fact the more I look at it the more it looks like the path they are following is perpendicular to the correct path, could there be some weird coordinates or incorrect transform information or something?

Is your enemy root transform (the one at the top of the chain) located at the same point as the rendered mesh or is the mesh object offset from this position? Attached is your script working in a simple test chamber, using the latest version of unity.

I suggest you start with the simplest possible working system and slowly add complexity until you understand what breaks it.

6469781–726095–test.unitypackage (4.42 KB)

Thank you for the advice; keeping it simple has helped, although I don’t really know what the original problem was it is working in a new project. I think it might have been due to having a camera at one position and the player object at another, on the same transform. Setting the player model GameObject as the target rather than the enclosing GameObject seems to do the trick, despite the transforms all looking correct at first glance.

1 Like