# How can I make an object move in a certain direction depending on its spawning point?

I'm currently developing a 2D game and I couldn't find a way to figure this out. In the game, I have 4 different balls and 4 different spawning points. Every 2 seconds, a random ball spawns at a random spawning point. What I want to achieve is, as I tried to describe above, lets say a ball spawns on right side, I want it to move towards left, if it spawns upside, I want it to go downwards etc.

Here is my spawner code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Game : MonoBehaviour {

public Transform[] spawnPoints;
public GameObject[] balls;

public float timer = 2.0f;

void Update()
{
timer -= Time.deltaTime;
SpawnBalls ();
}
void SpawnBalls()
{
int spawnPoints_num = Random.Range (0, 4);
int balls_num = Random.Range (0, 4);

if (timer <= 0)
{
Instantiate (balls [balls_num], spawnPoints [spawnPoints_num].position, spawnPoints [spawnPoints_num].rotation);
timer = 2.0f;
}
}
}


And here is my ball movement code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Balls : MonoBehaviour {

public Game game;
public float speed = 0.25f;

void Start ()
{
game = FindObjectOfType<Game> ();
}

void Update ()
{
//Some if statement
//transform.Translate (Vector3.left * Time.deltaTime * speed);
//else if
//transform.Translate (Vector3.up * Time.deltaTime * speed);
//else if
//transform.Translate (Vector3.right * Time.deltaTime * speed);
//else if
//transform.Translate (Vector3.down * Time.deltaTime * speed);
}
}


I've tried giving spawnpoints tags and creating if statements with gameObject.tag and stuff but couldn't find a way to work it. How can I make it check which spawnpoint the ball spawned at so I can make it move accordingly?

Thanks for help.

• Can't you just compare their spawn position with the center and make them move depending upon it?. – Aditya Yadav Mar 3 '17 at 6:59
• I guess I could do that but it feels like it's the long way to do it. I would want to learn the proper way so to speak. But that gave me an idea, maybe I can do something like if the spawnpoints' x or y value is greater or lower than a certain value, I can give Speed or -speed on x/y axis depending on those. – lorrotar Mar 3 '17 at 7:40

Get a reference to the spawn point. Orient its forward axis(the blue one) in the direction you want your spawned object to move and use this :

 transform.Translate(spawnPoint.transform.forward * Time.deltaTime * speed);

• This accomplished what I wanted but now I have a different problem. When I use: spawnPoints [spawnPoints_num].rotation on Instantiate, it moves as ı wanted but I'm unable to see the sprites, and if I use balls [balls_num].transform.rotation/localrotation, I see the sprites but they move in the wrong axis. – lorrotar Mar 3 '17 at 12:12

Ok, even though I couldn't manage to learn what I wanted, I figured a way out.

Instantiate (balls [balls_num], spawnPoints [spawnPoints_num].position,spawnPoints [spawnPoints_num].rotation, spawnPoints [spawnPoints_num]);


I added spawnPoints [spawnPoints_num] at the end of instantiate which makes the ball, children of the spawnpoint it got spawned at. After that I just gave spawnpoints tags and did a simple transform.parent.tag check, and go from there. Thanks everyone for their input.

What you're looking for is the Vector3.MoveTowards method. try the following

    public Transform target;
public float speed;
void Update()
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}

• I know how to do it by creating a game object and move the ball towards that. What I'm looking for is as I described, I'm looking for a way to make it detect and assign a ball to its spawn point so I can make it move towards where I want depending on the spawn point. – lorrotar Mar 3 '17 at 9:04
• what is the difference between each ball in the array of balls you created – Ido Ben Shalom Mar 3 '17 at 9:20
• I don't think MoveTowards is the right approach unless you have a specific target point. The asker wants to move it in a fixed direction, and that can and should be done without an artificial target object somewhere. – Christian Mar 3 '17 at 10:15
• @IdoBenShalom Nothing, just the colors – lorrotar Mar 3 '17 at 12:15