- Home /
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
$$anonymous$$y 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
@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.
Answer by aldonaletto · Oct 24, 2011 at 10:23 PM
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.
Answer by freeclup · May 31, 2013 at 08:10 AM
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);
}
Hey. i don't mean to be mean (no pun intended) but Awia asked for C#. Thats Javascript. Just sayin'