I am trying to access the object I instantiate - basically a prefab car instantiates cars (one after one) ..the problem that when try to move one .. they all move together ... what's the best way to separate them (and control them one after one)
the game is about parking .. the first car appear i choose which line to move to (target = empty-object) , then the second car appears and i choose which line also to move (the problem is here : when i hit the left/right-row all the cars go left and right .. i want only the last one to change the line )!! Any Ideas ... Thank You:)
this in Spawn.cs
public void SpawnCars()
{
if (spawnAllowed)
{
// choose a random strat point 1/6
randomSpawnPoint = Random.Range(0, spawnPoints.Length);
// choose a random car 1/6
randomSpawnCars = Random.Range(0, Cars.Length);
// clone randomly a car(1/6) from a random pre_set-points(1/6)
GameObject obj = Instantiate(Cars[randomSpawnCars], spawnPoints[randomSpawnPoint].position, Quaternion.Euler(-90, 180, 0)) as GameObject;
// carNumber will count how many cloned car
carNumber++;
// destroy the cloned car after N sec
// Destroy(obj, 8);
// every cloned car will take a tag with it
obj.gameObject.tag = "select" + k;
k++;
rcf = obj.GetComponent<RayCastForward>();
}
}
this in CarController.cs changing line problem (it change all the cars line)
void ChangeLine()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
f.RayDis();
if (f.v != "R6")
{
tf.transform.position = new Vector3(tf.position.x,tf.position.y,tf.position.z - 6.5f);
}
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
f.RayDis();
if (f.v != "R1")
{
tf.transform.position = new Vector3(tf.position.x, tf.position.y, tf.position.z + 6.5f);
}
}
the car will move to target after i click the UpRow key
// move the car to (target)
public void MoveCars(string go)
{
target = GameObject.Find(go);
directionToTarget = (target.transform.position - transform.position).normalized;
rb.velocity = new Vector3(directionToTarget.x * moveSpeed, directionToTarget.y * moveSpeed, directionToTarget.z * moveSpeed);
}