I dunno if the title is clear enough.
I want a game object to move towards another game object but once it reaches the object i want it to continue on its path in the same direction/alignment.
I have been trying to find a way to do this for a while now and i am stuck.
thanks
2 Answers
2
Supposing you want to use Update, and the object will start moving when “Fire1” is pressed, you can use the script below (attached to the moving object):
var target: Transform; // drag the target here
var speed: float = 5.0; // object speed
private var moving = false; // object initially stopped
private var dir: Vector3;
function Update(){
if (Input.GetButtonDown("Fire1")){ // if Fire1 pressed...
dir = target.position - transform.position; // calculate the target direction...
moving = true; // and enable movement
}
if (moving){ // if movement enabled...
// move the object in the calculated direction (world coordinates):
transform.Translate(dir * speed * Time.deltaTime, Space.World);
}
}
The object will start moving in the target object when Fire1 is pressed. The object will continue moving in the same direction, even if the target has moved from there. If Fire1 is pressed again, the object will turn to the new target direction and continue moving.
You will definitely find the answer these are just 4-5 minute clips.
How are you moving the object? Is it a rigid body with forces applied or are you doing a translate?(code sample?) Is the target game object also moving? Meaning are you intending to continuously update the direction of movement toward the target object? How are you determining when the object reaches the other object? collision? matching positions?
– Blazor_Ramonei am moving it using translate. all i want is to get the position of the target object and move the other object towards it and making it continue its path after it reaches the target object. what i mean by when it reaches the target object. is all i want is the other object to move in the direction of the target object but during that time the target object will move and not stay there. Think of it has a gun shot and someone moves out of the way but the gunshot doesnt stop it continues until it reaches a wall or something. thats what i wanna replicate.
– anon71668615