- Home /
Other
Moving an instantiated prefab from random point A to random point B
I have a prefab that is instantiated at a randomly chosen point (spawnPointStart) which is my Point A and another point is chosen (spawnPointEnd) which is Point B. I have tried using Lerp and MoveTowards along with invoke repeating and coroutines. When I run the game the object is instantiated. Sometimes the object stays still and other times the object will jump to random spots. I also looked into making a game object move in random directions. I want debris to appear and move randomly. I was hoping someone may have some suggestions.
Here's what I got (updated code with new attempts):
Debris Spawner
using UnityEngine;
using System.Collections;
public class DebrisSpawner : MonoBehaviour {
public static DebrisSpawner instance;
public GameObject[] debris;
public Transform[] debrisSpawnPoints;
private float speed;
private bool spawned;
//public GameObject debris;
//public Transform startSpawnPoint;
private float spawnTime;
// Use this for initialization
void Start () {
instance = this;
//spawnTime = Random.Range (5, 10);
//InvokeRepeating ("SpawnDebris", spawnTime, spawnTime);
}
// Update is called once per frame
void Update () {
/*if (RocketOrbit.buttonDown == true) {
CancelInvoke ("SpawnDebris");
}*/
if (spawned == false) {
StartCoroutine (AltDebrisSpawner ());
}
}
/*void SpawnDebris(){
speed = Random.Range (0.1f, 1.0f);
float randomSize = Random.Range (0.1f, 0.4f);
int spawnPointStart = Random.Range (0, debrisSpawnPoints.Length);
int spawnPointEnd = Random.Range (0, debrisSpawnPoints.Length);
int objectIndex = Random.Range (0, debris.Length);
GameObject newDebris = Instantiate (debris [objectIndex], debrisSpawnPoints [spawnPointStart].position, debrisSpawnPoints [spawnPointStart].rotation )as GameObject;
newDebris.transform.localScale = Vector3.one * randomSize;
Vector3 newPos = Vector3.MoveTowards (newDebris.transform.position, debrisSpawnPoints [spawnPointEnd].transform.position, speed);
newDebris.transform.position = newPos;
//newDebris.transform.position = Vector3.MoveTowards (newDebris.transform.position,
//debrisSpawnPoints [spawnPointEnd].transform.position, speed);
}*/
public IEnumerator AltDebrisSpawner(){
spawned = true;
speed = Random.Range (0.1f, 1.0f);
float randomSize = Random.Range (0.1f, 0.4f);
int spawnPointStart = Random.Range (0, debrisSpawnPoints.Length);
int spawnPointEnd = Random.Range (0, debrisSpawnPoints.Length);
int objectIndex = Random.Range (0, debris.Length);
GameObject newDebris = Instantiate (debris [objectIndex], debrisSpawnPoints [spawnPointStart].position, debrisSpawnPoints [spawnPointStart].rotation )as GameObject;
Vector3 targetTravel = new Vector3 (debrisSpawnPoints [spawnPointEnd].transform.position.x, debrisSpawnPoints [spawnPointEnd].transform.position.y, 0);
newDebris.transform.localScale = Vector3.one * randomSize;
/*Vector3 vectorToTarget = (debrisSpawnPoints[spawnPointEnd].position - newDebris.transform.position).normalized;
float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.up);
transform.rotation = Quaternion.Lerp(transform.rotation, q, Time.deltaTime * speed);*/
if (newDebris.transform.position != debrisSpawnPoints [spawnPointEnd].transform.position) {
newDebris.transform.position = Vector3.Lerp (newDebris.transform.position, targetTravel, speed);
Debug.Log ("Hey");
}
yield return new WaitForSeconds (4);
Destroy (newDebris);
spawned = false;
}
}
Answer by JC_SummitTech · Dec 07, 2016 at 03:32 AM
First, speed at line 34 is never used, might as well remove it. Second, Lerp just gives an interpolated value. you are currently setting your position once, on start. So nothing should move.
To have something occur overtime, you generally want to put it in Update() or PhysicsUpdate() Update is called every frame, so you could put your position setting code there.
Also, lerp takes 3 params, the start pos, the last pos, and a value between 0 (returning the startPos) and 1 (returning the end pos) so you don't want to give it deltaTime * speed or your return value will be pretty much the same each time, as deltaTime will be almost constant.