help with move towards 'javascript'

Hi,

I am trying to have objects move towards another. on the first frame everything will move towards the targeted position, but it will never update if the target moves. I tried adding an ‘InvokeRepeating’, but that doesn’t seem to help. I have teared down the code to this:

var target : Rigidbody;
var objects : Rigidbody;

InvokeRepeating('Update',1,1);

function Update ()
{ 
	rigidbody.transform.position = Vector3.MoveTowards(transform.position, target.transform.position, 0.03);
	rigidbody.transform.LookAt(target.transform);
}

any help would be appreciated!

is anything childed? im concerned your running into an issue where the coordinates your moving towards are local not global.

Time.time is terrible and you cant do that, thats the time since the game started. and Lerp expects a number between 0 and 1 to indicate distance. what you want is the time since the last frame

lerp(start.position,end.position, time.deltatime)

instantiate spawns an object at whatever the co-ordinates of the prefab are unless you change them.

if for example in this example now that you’ve given more information you want to spawn at the player and move towards this other object

missileprefab.transform.position = player.transform.position + player.transform.foward * .1 

//spawns 1/10th of a meter in front of the player
// so its not right on the player, realisiticly you
//should spawn from a point on the gun
// not the player but were debuggin

then have the tracking script on the missile and go towards target.

I’m not sure where you got this from, or what you are trying to do, but several things make my head clang !

  • A

First thing was brought up in a below comment : InvokeRepeating(‘Update’,1,1);

(The syntax is wrong, this should be written InvokeRepeating(“Update”,1,1); , use “Quotation marks”) - as corrected by Eric, you can use and " to denote strings. Ignore this line!

Now, I havn’t tested this, but Update runs every frame. To me this line would just invoke Update every second even while Update loops normally

You can set the time-step of FixedUpdate if that is required; 1/ any physics should be done in FixedUpdate , and 2/ the timestep can be modified in the editor.

You really should just make another function and use that :

InvokeRepeating( "MyMoveFunction" , 1.0, 1.0 );

function MyMoveFunction() { // move here }
  • B

Now to the objects, why are you typecasting as Rigidbody? Nothing in your script uses velocity, rather it uses objects’ transforms so why dont you just typecast and store references to the objects’ transforms instead.

I have never used MoveTowards but a quick search suggests that it is like lerp. So I guess you are trying to lerp between the current position and the target position. This could be done with Vector3.Lerp. Actually, the Unity Scripting Reference for Vector3.Lerp has as an example the exact script you are looking for :

I would use their example code like this :

var target : Transform;
var speed : float = 5.0;
function Update () {
    transform.position = Vector3.Lerp( transform.position, target.position, speed  * Time.deltaTime );
}

done =]

  • C

Think about what you want to make, then think about the most basic requirements, then work with that.

There is an alternative to your approach, just move the object towards the target at a constant speed, LookAt the target, then move forward :

var target : Transform;
var speed : float = 5.0;
function Update () {
    transform.LookAt( target );
    transform.position += transform.forward * speed * Time.deltaTime );
}

Hope this helps =]

Because most of content are in comments, I changed my answer with a summary.

  • You dont need of InvokeRepeating, Update is called each frame by engine.

  • For movements, is better to use FixedUpdate

  • Operations relative to time, like apply velocity, uses Time.fixedDeltaTime.
  • If you need to create many objects from a prefab to reference scene objects, dont change your prefab, instantiate a new one and then update it.
var playerShip : Transform;
var prefab : GameObject;

function SpawnAsteroid() {
    // create a new instance
	var asteroid = GameObject.Instantiate(prefab)
    // get script that track target and uptate its reference to scene ship
	asteroid.GetComponent(TargetScript).target = playerShip;
}