x


Moving an Object towards another Moving Object

HI
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 '11 at 10:01 PM

Awia gravatar image

Awia
16 1 1 1

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 '13 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 '13 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 '11 at 10:23 PM

aldonaletto gravatar image

aldonaletto
55.4k 21 65 261

(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 '13 at 08:10 AM

freeclup gravatar image

freeclup
0 3

(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

By RSS:

Answers

Answers and Comments

Topics:

x1556
x657
x271
x23

asked: Oct 24 '11 at 10:01 PM

Seen: 12920 times

Last Updated: Aug 25 '13 at 07:17 AM