0

I'm currently developing a game in Unity and I had a question. When spawning a new clone of a prefab that I created I want the instantiated game objects to move to the position that another gameobject yad at the time of their spawning. This means that by design I need to make different instances of the same gameobject/prefab behave differently or move to different positions even if they are active at the same time. However when going trough the options I have to go about this in my head I only get to extremely complex and very resource intensive solutions (For example creating a dictionary of Lists with each List holding the gameobjects created during it's creation and then making the velocity variable behave differently for different lists ext... ). However I feel like there must be a much simpler and less resource intensive solution since I see this type of problem being solved in games all the time. Does anyone know what I could do?

This is the code responsible for moving the gameobject instances

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

public class InteractMovement : MonoBehaviour
{

    Rigidbody2D rb;
    GameObject target;
    float moveSpeed;
    Vector3 directionToTarget;
    Renderer m_Renderer;

    void Start()
    {
        if (ScoreScript.scoreValue > 4)
        {
            target = GameObject.Find("White Ball");
            rb = GetComponent<Rigidbody2D>();
            moveSpeed = 5f; //Movement speed of all the obstacles and powerups
            InvokeRepeating("MoveInteract", 0f, 1f);
        }
    }

    void MoveInteract() //Method responsable for the movement of the obstacles and stars
    {
        if ( this.gameObject != null)
        {

            directionToTarget = (target.transform.position - transform.position).normalized;
            rb.velocity = new Vector2(directionToTarget.x * moveSpeed,
                                        directionToTarget.y * moveSpeed);


        }
        else
            rb.velocity = Vector3.zero;

    }




}

THis is the code respobsible for creating the instances of the gameobject in question:

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

public class BallSpawnerControl : MonoBehaviour
{

    public Transform[] spawnPoints;
    public GameObject[] interact;
    int randomSpawnPoint, Interact;
    int index = 1;
    public static bool spawnAllowed;

    // Use this for initialization
    void Start()
    {
        spawnAllowed = true;
        InvokeRepeating("SpawnAInteract", 0f, 1f);
    }

    void SpawnAInteract()
    {

        if (spawnAllowed)
        {

            if (index % 5 != 0)
            {
                randomSpawnPoint = Random.Range(0, spawnPoints.Length);
                Interact = 1;
Instantiate(interact[Interact], spawnPoints[randomSpawnPoint].position,
                    Quaternion.identity);
                index++;
            }
            else
            { 
            randomSpawnPoint = Random.Range(0, spawnPoints.Length);
              Interact = 0;
Instantiate(interact[Interact], spawnPoints[randomSpawnPoint].position,
                   Quaternion.identity);
                index++;
             }
        }

    }

}

The Ball and star (Gameobjects that follow) should move towards the position the whiteball held when they were created instead changing their direction towards the whiteball new invokation of MoveInteract

Maurice Bekambo
  • 325
  • 6
  • 21
  • 3
    This is a pretty basic use of class fields. Store the position of the white ball in `Start` and move towards that rather than moving towards the white ball's *current* position. This is by no means "different behavior" between instances. Its the same behavior *with different data.* – Draco18s no longer trusts SE Jul 19 '19 at 13:13
  • @Draco18s Ok ok, I just changed the code to reflect what you said. But for some reason the prefab clones now move switch back and forth between the position of the white ball they have in their data and an other point they previously corsed. Arent they supposed to move in a straight line past the position of the white ball in their data? – Maurice Bekambo Jul 19 '19 at 13:35
  • That entirely depends on how you handle the data. If you move *towards* the point, then it will never move *past.* If you want it to move *past* you need to compute things differently. Not change the data, but change how the data is *used.* – Draco18s no longer trusts SE Jul 19 '19 at 15:05

1 Answers1

2

Try storing its the target's position in a Vector3 variable in start

Vector3 position;

void Start()
{
    if (ScoreScript.scoreValue > 4)
    {
        target = GameObject.Find("White Ball");
        rb = GetComponent<Rigidbody2D>();
        position = target.transform.position;
        moveSpeed = 5f; //Movement speed of all the obstacles and powerups
        InvokeRepeating("MoveInteract", 0f, 1f);
    }
}


void MoveInteract() //Method responsable for the movement of the obstacles and stars
{
    if ( this.gameObject != null)
    {

        directionToTarget = (position - transform.position).normalized;
        rb.velocity = new Vector2(directionToTarget.x * moveSpeed,
                                    directionToTarget.y * moveSpeed);


    }
    else
        rb.velocity = Vector3.zero;

}
QWERTYL
  • 1,355
  • 1
  • 7
  • 11
  • @Draco18s Already tokd me to do the same thing however the clones now switch back and forth between the position of the white ball they have in their data and an other point they previously crosed. Arent they supposed to move in a straight line past the position of the white ball in their data? – Maurice Bekambo Jul 19 '19 at 14:55