0
\$\begingroup\$

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. Ball Movement 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.

\$\endgroup\$
  • \$\begingroup\$ Can't you just compare their spawn position with the center and make them move depending upon it?. \$\endgroup\$ – Aditya Yadav Mar 3 '17 at 6:59
  • \$\begingroup\$ 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. \$\endgroup\$ – lorrotar Mar 3 '17 at 7:40
1
\$\begingroup\$

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);
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – lorrotar Mar 3 '17 at 12:12
0
\$\begingroup\$

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.

|improve this answer|||||
\$\endgroup\$
-1
\$\begingroup\$

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);
    }
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – lorrotar Mar 3 '17 at 9:04
  • \$\begingroup\$ what is the difference between each ball in the array of balls you created \$\endgroup\$ – Ido Ben Shalom Mar 3 '17 at 9:20
  • \$\begingroup\$ 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. \$\endgroup\$ – Christian Mar 3 '17 at 10:15
  • \$\begingroup\$ @IdoBenShalom Nothing, just the colors \$\endgroup\$ – lorrotar Mar 3 '17 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.