1

The code helps in moving the game object in a continousy loop. I want the randomized generated cubes to follow the same pattern too. I didn't add the condition for stopping the generation of game objects when it completes one round. Currently, the generated game objects don't move.

The ultimate idea is to generate splash scene. I would like to know if the following way is gpu efficient too!

using UnityEngine;
using System.Collections.Generic;

public class IntegratedScrpt : MonoBehaviour
{
    public List<GameObject> splashImagesGOList;

    public float InvokeRate = 10f;
    public GameObject cube01;

    private int selection;

    // Loop mode variables
    private Vector3 startPosition;
    private Vector3 endPosition;
    private float distance = 54f;
    private float distanceCovered;
    private float currentDistance;

    //for Vector Lerp
    private float currentLerpTime = 0f;
    private float lerpTime = 9f;
    private float t;


    void Start()
    {
        startPosition = splashImagesGOList[1].transform.position;
        Debug.LogError("selection VALUE AT" + selection);

        endPosition = Vector3.back * distance;
    }

    void Update()
    {
        InvokeRepeating("pickpoints", 1.0f, InvokeRate);

        //loop mode
        distanceCovered = Vector3.Distance(startPosition, endPosition);
        currentDistance = distanceCovered * t;

        currentLerpTime += Time.deltaTime;
        if (currentLerpTime == lerpTime)
        {
            currentLerpTime = lerpTime;
        }
        if (currentLerpTime > lerpTime)
        {
            currentLerpTime = 0f;
        }
        t = currentLerpTime / lerpTime;

        Move();

        if (currentDistance == 64)
        {
            splashImagesGOList[selection].transform.position = startPosition;

            Move();
        }

        Debug.LogError("SELECTION" + selection);

    }

    // method for making the gameobjects move
    public void Move() 
    {
        splashImagesGOList[selection].transform.position = Vector3.Lerp(startPosition, endPosition, t);
    }

    // code for instantiating the gameobjects
    void pickpoints()  
    {
        foreach (GameObject cube01 in splashImagesGOList)
        {
            int selection = UnityEngine.Random.Range(0, splashImagesGOList.Count);
            // Instantiate(splashImagesGOList[selection], cube01.transform.position, cube01.transform.rotation);
            Instantiate(splashImagesGOList[selection], startPosition, cube01.transform.rotation);

        }
    }
}
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • 1
    What do you mean by "not working"? Does your code fail to compile? Does it unexpectedly format your hard drive? Please be more descriptive :) – Ruzihm Sep 05 '19 at 17:08
  • Well, it certainly hasn't formatted my hard disk... yet. :o :) I want the lerp function for each of my instantiated gameobjects @Ruzihm – ninjamac101 Sep 06 '19 at 10:59

1 Answers1

0

The reason the instantiated gameobjects aren't moving is because you're not assigning to any of their positions! splashImagesGOList[selection].transform.position is the position of one of the prefabs. In your current code, you instantiate the objects and then never interact with the instantiated objects.

You should have each object handle its own movement by separating out your movement logic into a different script and attaching the script to each of the prefabs in your list. You can use Mathf.Repeat to do the sort of looping your current code seems to mean to do.

Now, it's not clear what kind of pattern you are trying to achieve with the repeated simultaneous random instantiation but regardless of that, you probably don't mean to put InvokeRepeating in Update. Additionally, you should have some kind of end condition to cease the repeated PickPoints calls with CancelInvoke("PickPoints");. Creating an ever increasing number of objects is not gpu efficient ;)

Altogether, these changes might look like this:

public class SpashImageMover : MonoBehaviour
{
    public Vector3 startPosition;
    public Vector3 endPosition;
    public float3 lerpTime;

    private float t = 0; // in case code in Start is removed

    void Start()
    {
        // remove these two lines if you don't want the objects synchronized
        t = Mathf.Repeat(Time.time/lerpTime, 1f);
        transform.position = Vector3.Lerp(startPosition, endPosition, t);
    }

    void Update()
    {
        t = Mathf.Repeat(t + Time.deltaTime / lerpTime, 1f);

        transform.position = Vector3.Lerp(startPosition, endPosition, t);
    }
}

public class IntegratedScrpt : MonoBehaviour
{
    public List<GameObject> splashImagesGOList;

    public float InvokeRate = 10f;

    private int selection;

    // Loop mode variables
    private Vector3 startPosition;
    private Vector3 endPosition;

    //for Vector Lerp
    private float lerpTime = 9f;

    // end condition for PickPoints
    private bool invokingPickPoints;
    private float pickPointsTimeRemaining = 27f;


    void Start()
    {
        startPosition = splashImagesGOList[1].transform.position;
        Debug.LogError("selection VALUE AT" + selection);

        endPosition = Vector3.back * distance;

        InvokeRepeating("PickPoints", 1.0f, InvokeRate);
        invokingPickPoints = true;
    }

    void Update()
    {
        if (invokingPickPoints) 
        {
            pickPointsTimeRemaining -= Time.deltaTime;
            if (pickPointsTimeRemaining <= 0 ) 
            {
                CancelInvoke("PickPoints");
                invokingPickPoints = false;
            }
        }
    }

    // code for instantiating the gameobjects
    void PickPoints()  
    {
        foreach (GameObject cube01 in splashImagesGOList)
        {
            int selection = UnityEngine.Random.Range(0, splashImagesGOList.Count);
            // Instantiate(splashImagesGOList[selection], cube01.transform.position, cube01.transform.rotation);
            GameObject newGO = Instantiate(splashImagesGOList[selection], startPosition, cube01.transform.rotation);

            SpashImageMover mover = newGO.GetComponent<SpashImageMover>();
            mover.startPosition = startPosition;
            mover.endPosition = endPosition;
            mover.lerpTime = lerpTime;
        }
    }
}

As a sidenote, if you now find you don't like how you're instantiating the objects, that would be more appropriate for a different question with a very descriptive explanation of what you are trying to achieve. It's too broad of a question to try and address that here.

Ruzihm
  • 19,749
  • 5
  • 36
  • 48