Reputation: 17
im making a car runner game (all objects are in 3d) where in cops are chasing the player im spawning the cops from 4 different place (i.e left,right,top,bottom)
private int lastSpawnPos;
[SerializeField]
private Transform[] spawnPos;
void SpawnPoliceCar()
{
GameObject policeCar = ObjectPooling.instance.GetPooledObject("PoliceCar");
int r = UnityEngine.Random.Range(0, spawnPos.Length);
while (lastSpawnPos == r)
{
r = UnityEngine.Random.Range(0, spawnPos.Length);
}
Vector3 policeCarPos = policeCar.transform.position;
policeCarPos = new Vector3(spawnPos[r].position.x, 0, spawnPos[r].position.z);
policeCar.SetActive(true);
policeCar.GetComponent<Damage>().DefaultSetting();
lastSpawnPos = r;
currentPoliceCar++;
}
im calling this method in update() & this script is applied to empty game object in the scene. Since this code works perfectly fine
but now i wanted to add arrow indication on the screen where the cops are spawning as well as i wanted to rotate the arrow as per the direction. im spawning 4 cops at a time.
can any 1 help me in this pls im new to this platform & stuck here from long time
Upvotes: 0
Views: 76
Reputation: 1323
When you're spawning cops you can store them in the List<Transform>
cops and remove them (if it's supported to destroy or remove cops from the game). So you'll have player's position and all the cops positions.
Using this data you can find the distance from the cop to the player using Vector3.Distance or Vector2.Distance(if you want to ignore the z coordinate of player and cops). Using this methods you can loop throug cops List and find closets/farthest cop from the player (if you want to show not any cop but closest - the most recently spawned, or closest).
After you have found cop gameobject which you want the arrow to point to you can turn the arrow to this gameobject using Transform.LookAt method. Call this method every Update()
call and your arrow's rotation will follow selected cop.
Update: For the farthest cop your code can look smth like that:
private int lastSpawnPos;
[SerializeField]
private Transform[] spawnPos;
//Your arrow
public GameObject Arrow;
//Player
public Transform Player;
//Storing all spawned cops
public List<Transform> Cops;
void SpawnPoliceCar()
{
GameObject policeCar = ObjectPooling.instance.GetPooledObject("PoliceCar");
int r = UnityEngine.Random.Range(0, spawnPos.Length);
//Adding cop to the list when spawned
//TODO: do not forget to remove from the list, when cop is removed (back to the pool)
Cops.Add(policeCar.transform);
while (lastSpawnPos == r)
{
r = UnityEngine.Random.Range(0, spawnPos.Length);
}
Vector3 policeCarPos = policeCar.transform.position;
policeCarPos = new Vector3(spawnPos[r].position.x, 0, spawnPos[r].position.z);
policeCar.SetActive(true);
policeCar.GetComponent<Damage>().DefaultSetting();
lastSpawnPos = r;
currentPoliceCar++;
}
//Call this on update
void PointArrow()
{
Transform farthestCop = null;
float maxDistance = 0.0f;
//Find the farthes cop
foreach (var cop in Cops)
{
var distance = Vector3.Distance(Player.position, cop.position);
if (distance > maxDistance)
{
farthestCop = cop;
maxDistance = distance;
}
}
//If there are no cops - can't point an arrow
if(farthestCop == null) return;
//Point an arrow on the cop
Arrow.transform.LookAt(farthestCop);
}
I can't exactly say that it is what you are looking for without knowing the full game and code. Hope this will help
Upvotes: 2