- Home /
Moving a gameobject on a certain direction
Hi,
So, I have two gameobjects, a player and a crosshair. The crosshair is always 20 units in the forward direction of the player, with its y value set to 0. What I would like is to launch another gameobject, a projectile, from the player, to the crosshair. This is what I attempted.
When the player presses space, I run this little script
Instantiate( missile, transform.position, transform.rotation);
And in the missile start function I do this.
Vector3 direction = crosshair.transform.position - player.transform.position; direction.Normalize();
and in the Update function for missile, I do this
transform.Translate((direction moveSpeed Time.deltaTime));
But I get wierd results, it works when the player is facing a certain direction, but as it rotates, the direction keeps getting more and more inaccurate.
Any ideas?
If your problem is solved please accept an answer or close the question.
Answer by ExTheSea · May 08, 2013 at 08:11 PM
I don't think
Vector3 direction = crosshair.transform.position - player.transform.position; direction.Normalize();`
will give you the right results.
Why don't you use the transform.forward of your player? This Vector points in the direction the transform is facing.