- Home /
Moving instantiated prefab from randomly chosen startPoint to randomly chosen endPoint
I have been working on this for the last 4 days and after multiple attempts trying Lerp and MoveTowards I am still unable to make the instantiated prefab move from one point to the other. My issue is more complex than simply moving an object from one point to the other because the points are randomly chosen and my game object is an instantiated prefab. I am getting close, but I am having trouble figuring out what is causing this issue. Here's the issue in action - https://youtu.be/ZZ6F99TP3kc.
Here's my code:
DebrisSpawner:
using UnityEngine;
using System.Collections;
public class DebrisSpawner : MonoBehaviour {
public static DebrisSpawner instance;
GameObject newDebris;
public GameObject[] debris;
public Transform[] debrisSpawnPoints;
public Transform startPoint;
public Transform endPoint;
private float speed;
private bool spawned;
private int level;
private float spawnTime;
void Start () {
level = 0;
instance = this;
}
void Update () {
if (spawned == true && level == 0) {
level++;
float randomSize = Random.Range (0.1f, 0.4f);
int objectIndex = Random.Range (0, debris.Length);
//Debug.Log (startPoint);
newDebris = Instantiate (debris [objectIndex], startPoint.position, startPoint.rotation )as GameObject;
newDebris.transform.localScale = Vector3.one * randomSize;
Debug.Log (newDebris.transform.position);
//newDebris.transform.position = Vector3.MoveTowards (startPoint.transform.position, endPoint.transform.position, step);
}
if (spawned == false && level == 0) {
StartCoroutine (AltDebrisSpawner ());
int spawnPointStart = Random.Range (0, debrisSpawnPoints.Length);
int spawnPointEnd = Random.Range (0, debrisSpawnPoints.Length);
startPoint = debrisSpawnPoints [spawnPointStart];
endPoint = debrisSpawnPoints [spawnPointEnd];
}
}
public IEnumerator AltDebrisSpawner(){
spawned = true;
yield return new WaitForSeconds (4);
level--;
Destroy (newDebris);
spawned = false;
}
}
DebrisMoving:
using UnityEngine;
using System.Collections;
public class DebrisMoving : MonoBehaviour {
private float speed;
void Start () {
speed = 0.8f;
}
void Update () {
transform.position = Vector3.MoveTowards (DebrisSpawner.instance.startPoint.transform.position, DebrisSpawner.instance.endPoint.transform.position, 2);
Debug.Log (transform.position);
}
}
Answer by jmgek · Dec 12, 2016 at 06:56 PM
Is DebrisMoving attached to your prefab? If not
DebrisSpawner.cs
newDebris = Instantiate (debris [objectIndex], startPoint.position, startPoint.rotation )as GameObject;
newDebris.AddComponent<DebrisMoving>();
DebrisMoving.cs
using UnityEngine;
using System.Collections;
public class DebrisMoving : MonoBehaviour {
[SerializeField]
float speed = 0.8f;
void Update () {
float step = speed * Time.deltaTime;
//I don't see how your setting your endPoint position, to me you're never passing it a value.
transform.position = Vector3.MoveTowards(this.gameobject.transform.position, DebrisSpawner.instance.endPoint.transform.position, step);
}
}
Also, I do like to stay away from any physics but possibly a rigidbody.addforce sounds perfect for you: https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
Thanks for getting back to me! $$anonymous$$y Debris$$anonymous$$oving is attached to my prefab. Would a rigid body cause this issue (I have one attached to my prefabs and have kinematic checked)? I defined the end point on the DebrisSpawner Script on line 52 -
int spawnPointEnd = Random.Range (0, debrisSpawnPoints.Length); endPoint = debrisSpawnPoints [spawnPointEnd];
I then access this variable in the Debris$$anonymous$$oving Script:
transform.position = Vector3.$$anonymous$$oveTowards (DebrisSpawner.instance.startPoint.transform.position, DebrisSpawner.instance.endPoint.transform.position, 2);
I defined the end point on the DebrisSpawner Script on line 52 -
But where are you setting it on the Debris$$anonymous$$oving instance as far as I can tell you're only setting the end point in the DebrisSpawner?
It's a little confusing to me, why don't you let the Debris$$anonymous$$oving class handle the start and end?
newDebris = Instantiate (debris [objectIndex], startPoint.position, startPoint.rotation )as GameObject;
newDebris.endPos = //Put the end position here.
public class Debris$$anonymous$$oving : $$anonymous$$onoBehaviour {
public endPos;
}
It's working! The only thing I would like to add is a way to make the endpoint not equal to the start point. I tried a while loop, but when I tested I received unassigned reference errors due to the transform.$$anonymous$$oveTowards being in the update and requiring the endPos. This is not absolutely necessary, but would make the setup slightly better. If you have any thoughts I would appreciate it. If not, you answered my question and I thank you for your help!
Here my final code:
Debris$$anonymous$$oving
using UnityEngine;
using System.Collections;
public class Debris$$anonymous$$oving : $$anonymous$$onoBehaviour {
private float speed;
public Transform endPos;
public Transform startPos;
public static int level;
// Use this for initialization
void Start () {
level = 0;
speed = Random.Range(5,7);
}
// Update is called once per frame
void Update () {
Debug.Log (level);
if (DebrisSpawner.instance.spawned==true && DebrisSpawner.instance.level == 1 && level == 0) {
int spawnPointEnd = Random.Range (0, DebrisSpawner.instance.debrisSpawnPoints.Length);
endPos = DebrisSpawner.instance.debrisSpawnPoints [spawnPointEnd];
level++;
}
transform.position = Vector3.$$anonymous$$oveTowards (transform.position, endPos.transform.position, speed * Time.deltaTime);
}
}