- Home /
racer-detecting player position(1st,2nd,3rd,..)
I have four players running around a map how can I detect which one is coming first second third and fourth
Answer by RLin · May 02, 2015 at 04:10 PM
You can set up checkpoints around the map using triggers. Whoever is on the furthest checkpoint is in the lead, and if two people are going to the same checkpoint, you can get the distance to the next checkpoint, and the closer one is leading.
Thanks for your reply. I understand the logic behind how i should tackle the problem but I'm struggling to code it
Ive currently got a race$$anonymous$$anager script and a script on each player that talks to the race manager script letting it know there distance to next waypoint, when the collide into a waypoint and some other variables and it works with two players. The problem is i need 4 players and my code is just a cheap and easy work around
Answer by nugget70 · May 11, 2015 at 10:13 AM
using UnityEngine;
using System.Collections;
public class player : MonoBehaviour {
public float speed = 5.0f;
private int checkPointCounter;
private positionManager PositionManager;
public Transform currentCheckPoint, nextCheckPoint;
// Use this for initialization
void Start () {
PositionManager= GameObject.Find("Main Camera").GetComponent<positionManager>();
}
// Update is called once per frame
void Update () {
nextCheckPoint = PositionManager.checkPoints[checkPointCounter];
PositionManager.p1Distance = Vector3.Distance(transform.position,nextCheckPoint.transform.position);
float targetSpeed=Input.GetAxisRaw("Horizontal")*speed;
if (Input.GetKey("d")){
transform.Translate(new Vector2(targetSpeed * Time.deltaTime, 0), Space.World);
}else if (Input.GetKey("a"))
{
transform.Translate(new Vector2(targetSpeed * Time.deltaTime, 0), Space.World);
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag=="checkPoint") {
if (checkPointCounter ==4)
checkPointCounter = -1;
}
checkPointCounter++;
PositionManager.checkPointPlayer1 = checkPointCounter;
}
}
Answer by nugget70 · May 11, 2015 at 10:13 AM
using UnityEngine;
using System.Collections;
public class positionManager : MonoBehaviour {
public GameObject p1, p2;
private player Player;
private player2 Player2;
public float p1Distance, p2Distance;
public int checkPointPlayer1, checkPointPlayer2;
public string [] playerPos={"1st","2nd"};
public Transform [] checkPoints;
public bool diffCheckPoint;
// Use this for initialization
void Start () {
p1 = GameObject.Find("player1");
p2 = GameObject.Find("player2");
Player = p1.GetComponent<player>();
Player2 = p2.GetComponent<player2>();
}
// Update is called once per frame
void Update () {
if (checkPointPlayer1 == checkPointPlayer2)
{
if (p1Distance < p2Distance)
{
p1.GetComponentInChildren<TextMesh>().text = playerPos[0];
p2.GetComponentInChildren<TextMesh>().text = playerPos[1];
}
else {
p1.GetComponentInChildren<TextMesh>().text = playerPos[1];
p2.GetComponentInChildren<TextMesh>().text = playerPos[0];
}
diffCheckPoint = false;
}
else {
diffCheckPoint = true;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613194429im_/https://answers.unity.com/themes/thub/images/avi.jpg)