Moving a gameObject towards the player..

Hi,

I have a bit of a problem with a script I’m currently using.
I want a gameObject to move towards me, which works, but the closer he gets, the slower he becomes, which is something I’m not to keen on…

Here’s the part of the script:

		transform.LookAt(player);
		transform.eulerAngles.x = 0;
		var target : Vector3 = GameObject.FindWithTag("Player").transform.position;
		var moveDirection : Vector3 = target - transform.position;
		moveDirection.y = 0;
		gameObject.rigidbody.velocity = moveDirection * Time.deltaTime * speed;

As you can see, the velocity is calculated from the current position of the player minus the position of the gameObject, so it makes sense that the gameObject becomes slower the closer it gets to the player, but I couldn’t find an alternative to that yet.

If anybody has a solution at hand, that doesn’t even involve a RigidBdody component, I’d be more than happy to try it, because this script is the only reason I’m using a RigidBody anyway…

Thanks…

/edit: Just came across my mind, even though it’s not related to the original question. Can someone explain to me, how I’m using Bounds properly? I’d like to instantiate an object with it’s lowest point at another objects lowest point, so I figured Bounds were the way to go, but the Bounds section in the scripting reference isn’t exactly bursting with code examples… :o

Regarding the ‘movement speed’ problem, the solution you’re looking for is to normalize ‘moveDirection’ before using it to move the object (and after setting its ‘y’ element to 0).

The only time this will fail is if the vector prior to normalization is the zero vector or has very small magnitude; this can happen if the object in question is more or less at the same position as the player, or is more or less directly above or below the player. If you know this won’t happen in practice, you can ignore this case; otherwise, you might want to check the magnitude of the vector against a threshold and skip the movement if it’s below this threshold.