Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » Raycast going through other objects

Raycast going through other objects

View previous topic View next topic Go down  Message [Page 1 of 1]

1 Raycast going through other objects on Sun Feb 14, 2016 4:56 pm

2h


Member [Lv. 0]
Member [Lv. 0]
Hi Folks, new to the forum and Unity

I am having a problem with raycasts.

I have a small sandbox setup. I have a patrol cycling through a series of waypoints. The patrol has a detection zone around it. OnTriggerStay, the patrol attempts to fire a raycast at the object detected, if that object is the player. The patrol then follows the player if it has line of sight.

I would like at this stage that the player can move and hide behind walls etc. However, my raycasts are going through other objects even though they have colliders on them.

I have worked on this all with, with different approaches but cannot get around the issue.

Help would be much appreciated.

I attach here copies of my PatrolDetection script and the patrolController script.

PatrolController...

Code:
using UnityEngine;
using System.Collections;
//This allows for the use of lists
using System.Collections.Generic;

public class PatrolController : MonoBehaviour {
 
 public List<Transform> wayPointsList = new List<Transform>();

 public Transform currentWaypoint;

 public Transform currentPatrolTarget;

 public int wayPointNumber = 0;
 
 public float speed =4f;

 public float turnSpeed = 1f;

 public PatrolDetection PT;

 //NavMeshAgent Componants -> Navigation
 public NavMeshAgent navAgent;

 // Use this for initialization
 void Start () {
 currentWaypoint = wayPointsList [wayPointNumber];
 }
 
 // Update is called once per frame
 void Update () {
 moveEnemy ();
 }
 
 public void moveEnemy()
 {
 if (PT.isDetected == true && PT.canSee == true)
 {
 //Debug.Log ("The Player is detected and seen!");
 currentPatrolTarget = GameObject.Find ("Player").transform;
 navAgent.SetDestination(currentPatrolTarget.position);
 }
 else
 {
 if(navAgent.remainingDistance <= navAgent.stoppingDistance)
 {
 if (wayPointNumber != wayPointsList.Count - 1) {
 wayPointNumber++;
 navAgent.SetDestination(wayPointsList [wayPointNumber].position);
 }
 else
 {
 wayPointNumber = 0;
 navAgent.SetDestination(wayPointsList [wayPointNumber].position);
 }

 }
 }
 }

}

PatrolDetection

Code:
using UnityEngine;
using System.Collections;

public class PatrolDetection : MonoBehaviour {

 public LayerMask sightBlockingLayers;

 public RaycastHit hit;

 public float maxRayCastRange = 100.0f;

 //general detection
 public bool isDetected = false;
 //can see target
 public bool canSee = false;
 
 void OnTriggerStay(Collider other)
 {
 //Something has triggered Patrol Detection
 isDetected = true;
 if(other.gameObject.CompareTag("Player"))
 {
 //Debug.Log ("The " + other + " is detected");
 if (Physics.Raycast(transform.position, (other.transform.position - transform.position), out hit, sightBlockingLayers));
 {
 Debug.DrawLine(transform.parent.position, hit.point);
 canSee = true;
 //Debug.Log ("The " + other + " is seen");
 }
 }
 }

 void OnTriggerExit(Collider other)
 {
 isDetected = false;
 if(other.gameObject.name == "Player")
 {
 //Debug.Log("The " + other + " exited the collider");
 canSee = false;
 }
 }
}

Any help would be greatly appreciated, I'm quite stuck.

2h

View user profile

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum