-1

I have a cone shape with 3 raycasts, but the enemy only detect the player if the player is in either one of the 3 lines, i wanted to add more raycast to the enemy. I have ask around, some said to add more raycast with for loop, but im not familiar with raycasting, so i need help. the photo attachment below is what i have right now and what i want the ai raycast to be.

EnemyRaycastImage

public class enemyControl : MonoBehaviour
{
    Ray enemyRay;
    public Color rayColor;
    RaycastHit rayHit;
    bool follow;
    public float sightDist;
    private float timer = 0f;
    public float heightMultiplier;
    private Vector3 investigateSpot;
    public float visionAngle;

    private NavMeshAgent agent;
    public GameObject him;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        him = GameObject.FindGameObjectWithTag("Player");

        heightMultiplier = 1.36f;
    }

    void Update()
    {
        timer += Time.deltaTime;
        enemyRay = new Ray(transform.position, transform.forward * sightDist);
        Debug.DrawRay(transform.position + Vector3.up * heightMultiplier, transform.forward* sightDist, rayColor);
        Debug.DrawRay(transform.position + Vector3.up * heightMultiplier, (transform.forward  + transform.right).normalized * sightDist, rayColor);
        Debug.DrawRay(transform.position + Vector3.up * heightMultiplier, (transform.forward - transform.right).normalized * sightDist, rayColor);


        if (Physics.Raycast(transform.position + Vector3.up * heightMultiplier, transform.forward, out rayHit, sightDist))
        {
            if (rayHit.collider.gameObject.tag == "Player")
            {
                agent.SetDestination(him.transform.position);
                him = rayHit.collider.gameObject;
            }
        }


        if (Physics.Raycast(transform.position + Vector3.up* heightMultiplier, (transform.forward + transform.right).normalized, out rayHit, sightDist))
        {
            if (rayHit.collider.gameObject.tag == "Player")
            {
                agent.SetDestination(him.transform.position);
                him = rayHit.collider.gameObject;
            }
        }

        if (Physics.Raycast(transform.position + Vector3.up* heightMultiplier, (transform.forward - transform.right).normalized, out rayHit, sightDist))
        {
            if (rayHit.collider.gameObject.tag == "Player")
            {
                agent.SetDestination(him.transform.position);
                him = rayHit.collider.gameObject;
            }
        }
    }

    void OnTriggerEnter(Collider coll)
    {
        if (coll.tag == "Player")
        {
            agent.SetDestination(him.transform.position);
            him = coll.gameObject;
        }
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Gallant
  • 1
  • 1
  • I'm not answering the question, but I can give you an advice before you proceed. First of all you should first understand how a loop for works in C#. This is pretty simple and you will be able to find many tutorials around. Regarding raycasting, follow Unity's documentation and tutorial on how to use it. Also, do not expect to be able to develop a good 3D game without first understanding the basics of analytic geometry, it is sort of mandatory knowledge. – sɐunıɔןɐqɐp Oct 20 '18 at 07:14
  • @Gallant Well i think you don't need more raycasts, you can detect raycast hit of all 3 raycasts you have already and if one of them had a hit, you will know that enemy got hit. – Amir Oveisi Oct 20 '18 at 08:23

1 Answers1

1

Tried it and works for me, it can detect player that is colliding with multiple raycast. But it is hard to position the player into this position Collision position since it is close to ray origin. Debugged and works Ray Debug

Native variables from the script

public Color rayColor;
RaycastHit rayHit;
public float sightDist;
public float heightMultiplier;
public float visionAngle;
public GameObject him;

I add these variables:

private bool rayIsDrawn = false;
[SerializeField]
private int raycastCount = 10;
private float RightRay;
private float LeftRay;

The `raycastCount' defines the total number of rays.

void Start()

enter code here
void Start()
{
    him = GameObject.FindGameObjectWithTag("Player");
    heightMultiplier = 1.36f;
}

void Update(). Here's the code to draw ray. Ray0 is drawn straight forward, while next rays are drawn right and left depending on i number; I set the y axis of ray direction to visionAngle / 2 - visionAngle / (raycastCount-1) * RightRay for right ray and -visionAngle / 2 + visionAngle / (raycastCount-1) * LeftRay for left ray.

void Update()
{
    if (rayIsDrawn == false) {
        for (int i = 0; i < raycastCount; i++) {
            if (i == 0) {
                Debug.DrawRay (transform.position + Vector3.up * heightMultiplier, transform.forward * sightDist, rayColor, Mathf.Infinity);
            } else if (i % 2 != 0) {
                Debug.DrawRay (transform.position + Vector3.up * heightMultiplier, Quaternion.Euler (0, visionAngle / 2 - visionAngle / (raycastCount-1) * RightRay, 0) * transform.forward * sightDist, rayColor, Mathf.Infinity);
                RightRay++;
            } else {
                Debug.DrawRay (transform.position + Vector3.up * heightMultiplier, Quaternion.Euler (0, -visionAngle / 2 + visionAngle / (raycastCount-1) * LeftRay, 0) * transform.forward * sightDist, rayColor, Mathf.Infinity);
                LeftRay++;
            }
        }
        rayIsDrawn = true;
    }
    RightRay = 0;
    LeftRay = 0;

And this is the raycast with same ray origin and direction with the drawn rays.

    for (int i = 0; i < raycastCount; i++) {
        if (i == 0) {
            if (Physics.Raycast (transform.position + Vector3.up * heightMultiplier, transform.forward, out rayHit, sightDist)) {
                if (rayHit.collider.tag == "Player") {
                    Debug.Log ("touched by ray "+(i+1));
                }
            }
        } else if (i % 2 != 0) {
            if (Physics.Raycast (transform.position + Vector3.up * heightMultiplier, Quaternion.Euler (0, visionAngle / 2 - visionAngle / (raycastCount-1) * RightRay, 0) * transform.forward, out rayHit, sightDist)) {
                if (rayHit.collider.tag == "Player") {
                    Debug.Log ("touched by ray "+(i+1));
                }
            }
            RightRay++;
        } else {
            if (Physics.Raycast (transform.position + Vector3.up * heightMultiplier, Quaternion.Euler (0, -visionAngle / 2 + visionAngle / (raycastCount-1) * LeftRay, 0) * transform.forward, out rayHit, sightDist)) {
                if (rayHit.collider.tag == "Player") {
                    Debug.Log ("touched by ray "+(i+1));
                }
            }
            LeftRay++;
        }
    }
    RightRay = 0;
    LeftRay = 0;    
}

enter image description here

Noblight
  • 136
  • 5
  • This is really nice. Although it would easier to generate the positions in an array, loop over them and do the raycast based on them. It removes the unnecessary `if` statements. – Programmer Oct 20 '18 at 16:18