Before updating to Windows 10 you may need to return your Pro license. More information here

Moving an Object towards another Moving Object

How do I move an object (particle-emitter) towards the player who are most certainly moving around? I have no idea on how I should do this, and I could not find an answer to this in the answered questions.
My game is made in c#, and I'm pretty new to programming so be nice :D

more ▼

asked Oct 24, 2011 at 10:01 PM

avatar image

16 1 2 2

My object to move is lifting off the ground to get to target, and when i add a rigidbody, it makes it fall over and glitch/bug

Aug 25, 2013 at 07:15 AM Casper091

@Casper091 - If you have a problem, please open a new question. Your comment is not related to this question. I'll be deleting your comment shortly.

Aug 25, 2013 at 07:17 AM robertbu
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You must repeat the same sequence at each Update: get the target position, then move the object in the target direction a small distance proportional to the desired speed and to the time elapsed since the last frame (Time.deltaTime). The easiest way to do that is with Vector3.MoveTowards (badly explained in the docs):

Transform target; // drag the player here float speed = 5.0f; // move speed

void Update(){ transform.position = Vector3.MoveTowards(transform.position, target.position, speed*Time.deltaTime); } Vector3.MoveTowards(from, to, distance) returns a point in the line from-to distant distance units from the the from point. The distance parameter is internally clamped to never return a point ahead the to point.
NOTE: This script must be attached to the particle object.

more ▼

answered Oct 24, 2011 at 10:23 PM

avatar image

61.6k 57 213 361

(comments are locked)
10|3000 characters needed characters left

just attache object and set target

 #pragma strict
   // drag the player here
 var  speed:float = 5.0f; // move speed
 function Start () {

 function Update () {
  transform.position = Vector3.MoveTowards(transform.position, Vector3(32,15,-92),   speed*Time.deltaTime);
more ▼

answered May 31, 2013 at 08:10 AM

avatar image

16 895 7

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Oct 24, 2011 at 10:01 PM

Seen: 24734 times

Last Updated: Aug 25, 2013 at 07:17 AM