- Home /
Make a game object move towards another object and continue its path.
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
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? $$anonymous$$eaning are you intending to continuously update the direction of movement toward the target object?
How are you deter$$anonymous$$ing when the object reaches the other object? collision? matching positions?
i 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.
Answer by aldonaletto · Mar 02, 2012 at 04:50 PM
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.
Answer by WaleedAzam · Aug 17, 2014 at 02:23 PM
http://www.youtube.com/watch?v=VcNly-cMZV4
http://www.youtube.com/watch?v=mP7ulMu5UkU
You will definitely find the answer these are just 4-5 minute clips.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
scaling plane gameobject to fit orthographic view iphone unity3d 0 Answers
Continually Updating position of Instantiated Object (to Mouse Pointer) 0 Answers
DontDestory gameobject doesn't work on button click after a new scene loads 2 Answers
Unity Detonator Problems 1 Answer