Making randomly instantiated objects all fly through a single point?

Hey, I’m trying to make a game where there is a single “target sphere” at the center of the screen, and a bunch of other spheres come flying in from off-screen, and the object of the game is to hit the space bar when the flying spheres pass through the target sphere (think Dance Dance Revolution, except with only one button and in 3D).

Now I’ve already got all the flying spheres to instantiate at random positions and all move towards the the target sphere, but the problem I’m having is that they stop once they’ve arrived at the center, and I want them to keep going and fly off-screen if the player doesn’t hit the space bar at the appropriate time.

So the question is: how do I make an object face another object and move towards it, but not stop when it arrives at the target object, and instead continue moving forward with that same speed and trajectory?

Here’s my code:

private var target : GameObject; 
var speed : float = 1; 

function Start () 
{
   target = GameObject.FindGameObjectWithTag("Target"); 
} 

function Update() 
{
   var distance = Vector3.Distance( target.transform.position, transform.position);
   var delta = target.transform.position - transform.position;
   delta.Normalize();
   var moveSpeed = speed * Time.deltaTime;
   transform.position = transform.position + (delta * moveSpeed);

   if (distance < 0.1  Input.GetKeyDown(KeyCode.Space))
   {
     Destroy (gameObject);
     scoreBox.score++;
   }
}

Assuming the target doesn’t move, compute the normalized delta once only (e.g. in your Start() function).

Just tried that, and it stopped the spheres from moving entirely. :frowning:

Then you must not have done it right :slight_smile:

If you can post your code, I imagine someone will be able to spot the problem (or at least help you isolate it).

Here’s the code after I change it from above:

private var target : GameObject; 
var speed : float = 1; 

function Start () 
{
   target = GameObject.FindGameObjectWithTag("Target");
   var delta = target.transform.position - transform.position;
   delta.Normalize();
   var moveSpeed = speed * Time.deltaTime;
   transform.position = transform.position + (delta * moveSpeed);
} 

function Update() 
{
   var distance = Vector3.Distance( target.transform.position, transform.position);

   if (distance < 0.1  Input.GetKeyDown(KeyCode.Space))
   {
     Destroy (gameObject);
     scoreBox.score++;
   }
}

You are only moving the object once, in the Start function.

I adjusted your code to (very) slightly improve performance, you needed to store your movement variable, and change your position every frame, in the Update function.

Here is the code:

private var target : GameObject; 
private var movement : Vector3;
var speed : float = 1; 

function Start () 
{
   target = GameObject.FindGameObjectWithTag("Target");
   var delta : Vector3 = target.transform.position - transform.position;
   delta.Normalize();
   movement = delta * speed;
} 

function Update() 
{
   transform.position = transform.position + (movement * Time.deltaTime);

   var distance = Vector3.Distance( target.transform.position, transform.position);

   if (distance < 0.1  Input.GetKeyDown(KeyCode.Space))
   {
     Destroy (gameObject);
     scoreBox.score++;
   }
}