Luke
Luke

Reputation: 25

Raycasting Issue Unity

I need to have a reading of weather the player car is coming 1st, 2nd or third against 2 AI cars, I have tried to accomplish this using ray casting to no avail. I have waypoints in the level periodically and have a system that sets the new waypoint when the AI or player enter it, it then draws a ray to the next checkpoint so that I can calculate its distance, using hit.distance and then assigns that distance every update to a variable float. However it is not getting the order right and when I am coming first it displays all possible combinations in debug. The main problem I think comes when I debug the distance, im right next to the waypoint and it might say 300, or it could say 0.003447 or something and it's really confusing me.

Below is the script for casting the rays:

    using UnityEngine;
using System.Collections;

public class RaceManager : MonoBehaviour {
    public GameObject seeking;
    public float distance1;
    public float distance2;
    public float distance3;
    public int playerPos;

    void Update (){
        Debug.Log (playerPos);
        //Firing Rays to the target every update and recording its distance.
        if (this.gameObject.name == "Player") {
            RaycastHit hit;
            Ray playerRay = new Ray (this.gameObject.transform.position, seeking.transform.position);
            if (Physics.Raycast (playerRay, out hit)) {
                distance1 = hit.distance;
                Debug.DrawLine (this.transform.position, seeking.transform.position);
            }
        }
        if (this.gameObject.name == "AICar1"){
        RaycastHit hit;
        Ray playerRay = new Ray (this.gameObject.transform.position, seeking.transform.position);
        if (Physics.Raycast (playerRay, out hit)){
                distance2 = hit.distance;
                Debug.DrawLine(this.transform.position, seeking.transform.position);
        }
    }
        if (this.gameObject.name == "AICar2") {
        RaycastHit hit;
        Ray playerRay = new Ray (this.gameObject.transform.position, seeking.transform.position);
            if (Physics.Raycast (playerRay, out hit)){
                distance3 = hit.distance;
                Debug.DrawLine(this.transform.position, seeking.transform.position);

            }
        }

        //Decide placement based on Ray Length.
        //If Player is first.
        if (distance1 < distance2 && distance1 < distance3) {
                playerPos = 1;
            }
        else if (distance1 < distance2 && distance1 > distance3 || distance1 > distance2 && distance1 < distance3) {
                playerPos = 2;
        }
        else {
                playerPos = 3;
        }
    }
}

And here is the script responsible for changing the target when the player or AI get to it

    using UnityEngine;
using System.Collections;

public class RaceManager : MonoBehaviour {
    public GameObject seeking;
    public float distance1;
    public float distance2;
    public float distance3;
    public int playerPos;

    void Update (){
        Debug.Log (playerPos);
        //Firing Rays to the target every update and recording its distance.
        if (this.gameObject.name == "Player") {
            RaycastHit hit;
            Ray playerRay = new Ray (this.gameObject.transform.position, seeking.transform.position);
            if (Physics.Raycast (playerRay, out hit)) {
                distance1 = hit.distance;
                Debug.DrawLine (this.transform.position, seeking.transform.position);
            }
        }
        if (this.gameObject.name == "AICar1"){
        RaycastHit hit;
        Ray playerRay = new Ray (this.gameObject.transform.position, seeking.transform.position);
        if (Physics.Raycast (playerRay, out hit)){
                distance2 = hit.distance;
                Debug.DrawLine(this.transform.position, seeking.transform.position);
        }
    }
        if (this.gameObject.name == "AICar2") {
        RaycastHit hit;
        Ray playerRay = new Ray (this.gameObject.transform.position, seeking.transform.position);
            if (Physics.Raycast (playerRay, out hit)){
                distance3 = hit.distance;
                Debug.DrawLine(this.transform.position, seeking.transform.position);

            }
        }

        //Decide placement based on Ray Length.
        //If Player is first.
        //All signs reversed for testing
        if (distance1 < distance2 && distance1 < distance3) {
                playerPos = 1;
            }
        else if (distance1 < distance2 && distance1 > distance3 || distance1 > distance2 && distance1 < distance3) {
                playerPos = 2;
        }
        else {
                playerPos = 3;
        }
    }
}

Upvotes: 1

Views: 513

Answers (1)

Joshua Wade
Joshua Wade

Reputation: 61

If you know the position of the waypoint of interest as well as the positions of all of the vehicles in the environment, then just use the static distance function in the Vector3 class Vector3.Distance. Just stash references to the vehicles' transforms, then measure the distance between each vehicle and the relevant waypoint. There is a ton more overhead using ray casting methods to do what you want to do.

Upvotes: 2

Related Questions