- Home /
Bug with navigation and raycasting
I'm having a problem with raycasting on objects that have a NavMeshAgent. After about 15-20 seconds or so I can no longer target my unit GO. It's as though the collider for that GO has disappeared. I saw that this issue was brought up about a year and a half ago but is it still a known issue/bug? Am I better off with A* ? Thanks.
P.S. I posted this in the forums but couldn't get a reply and I'm really stuck here.
Ok never heard of that - but could be a fault - if you look at the scene while you are playing can you see the collider in place on the agent? If you Debug.DrawRay your raycast does it change or not intersect for some reason?
I added a screenshot of the DrawRay. Goes right through the object. It works for the first little bit. One thing I have noticed is that at first I can click anywhere on the model and it selects it, but eventually you have to move further down the body to get it to select. Thanks for the reply, very frustrated with this problem.
(Converted your "answer" to a comment - hit the add new comment button hiding on the right if you aren't posting a solution).
Yep that is definitely very strange. Would you $$anonymous$$d posting your code for the raycast and draw ray just so I can sanity check it for you? Is that a compound collider or is it possible you are hitting a different collider first with the ray?
This class has a fair bit going on so I'm not sure if you really dive too far into it.
using UnityEngine;
using System.Collections;
public class $$anonymous$$ouseControls: $$anonymous$$onoBehaviour {
RaycastHit hit;
//gameobject selection arrays/variables
public static ArrayList currentSelectUnits = new ArrayList();//arrayList of Crew objects
public GameObject currentUnit;
public static ArrayList unitsOnScreen = new ArrayList();
//Right-click destination object variables
public GameObject crewDest;
public GameObject destObj;
//private float destBoxSize = 15f;
private GameObject[] crewDestArray;
//clicking variables
private static Vector3 leftDownPoint;
private static float clickDragZone = 1.3f;
private static float doubleClickDelay = .35f;
public float doubleClickRemain;
private bool one_click = false;
public bool checkRotating;
private Vector3 rightDownPoint;
public float rotateClickTimer = 0f;
private int addOn = 5;
void Awake()
{
leftDownPoint = Vector3.zero;
checkRotating = false;
rightDownPoint = Vector3.zero;
crewDest = Instantiate(destObj, new Vector3(10,0,10),transform.rotation) as GameObject;
crewDestArray = new GameObject[2];
crewDestArray[0] = crewDest;
for(int i = 1;i<2;i++)
{
crewDestArray[i] = Instantiate(destObj, new Vector3(10,0,10 + (addOn * i)),transform.rotation) as GameObject;
}
}
void Update () { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(ray, out hit,$$anonymous$$athf.Infinity)) { Debug.DrawLine (ray.origin,ray.direction * 5000,Color.red);
if(Input.Get$$anonymous$$ouseButtonDown(0))//when user first clicks the mouse down, potentially starting a mouse drag
{
leftDownPoint = hit.point;
if(!one_click)//first click
{
one_click = true;
doubleClickRemain += Time.deltaTime;
}
else
{
if(doubleClickRemain > doubleClickDelay)
{
//basically if thats true its been too long and we want to reset so the next click is simply a single click and not a double click.
one_click = false;
}
else//this is a double click
{
}
}
}
//check to see if enough time has passed in order to remove the first click, if there is one
//$$anonymous$$ouseClick
else if(Input.Get$$anonymous$$ouseButtonUp(0) && didUserClickLeft$$anonymous$$ouse(hit.point))
{
if(hit.collider.name == TagName.ground_name)
{
deselectGameObjectIfSelected();
}
else if(hit.collider.tag == TagName.gangster_tag)
{
//Debug.Log("Parent: " + hit.transform.parent.GetComponent<Crew>().gangList.Length);
if(!unitAlreadySelected(hit.collider.gameObject))//If unit is not already selected
{
//Get the Crew script from the selected gangster, then select the entire Crew and add the script to the currentSelectUnits list
//moving and setting the selection should all be done within the Crew script.
if(!shift$$anonymous$$eysDown())//if the neither of the shift keys are pressed
{
deselectGameObjectIfSelected();
currentUnit = hit.collider.gameObject;
Crew c = currentUnit.transform.parent.GetComponent<Crew>();
c.SetSelected(true);
//currentUnit.transform.Find(TagName.select_display).gameObject.SetActive(true);
currentSelectUnits.Add(c);
}
else
{
if(unitAlreadySelected(hit.collider.gameObject))
{
removeUnitFromSelected(hit.collider.gameObject);
}
else
{
currentUnit = hit.collider.gameObject;
Crew c = currentUnit.transform.parent.GetComponent<Crew>();
c.SetSelected(true);
//currentUnit.transform.Find(TagName.select_display).gameObject.SetActive(true);
currentSelectUnits.Add(c);
}
}
}
}//end of if hit collider crew
}//end of mouse click input
#endregion
#region right click
else//if left mouse button has not been clicked, then compute right click stuff
{
if(Input.Get$$anonymous$$ouseButtonDown(1))
{
rightDownPoint = hit.point;
}
else if(Input.Get$$anonymous$$ouseButton(1))
{
if(!checkRotating)
rotateClickTimer += Time.deltaTime;
if(rotateClickTimer > .5f && !checkRotating)
{
checkRotating = true;
}
if(checkRotating)
{
crewDest.transform.LookAt(hit.point);
}
}
Answer by unimechanic · Feb 24, 2014 at 09:19 PM
To answer your last comment:
So when I turn off the NavMeshAgent selection works just fine and the problem seems to be gone. Anyone know what about the NavMeshAgent that could be causing this or should I report a bug?
Yes, please submit a bug report with a minimal sample project (if possible, will be very helpful) that reproduces this problem, following these guidelines and additional information:
http://unity3d.com/BugReportingFAQ http://blogs.unity3d.com/2013/10/28/bug-reports-incidents-and-some-bashing/ http://issuetracker-staging.unity3d.com/
The bug reporting tool is next to the Unity executable if you can't run it from the editor. Our QA team will review the issue as soon as possible, and then assign it to our developers. Having the bug report is the only way this problem can be fixed.
Your answer
Follow this Question
Related Questions
raycast not colliding 1 Answer
Multiple hit detection with Raycasting? 2 Answers
Raycast not hitting collider 2 Answers
Fixing Editor Mouse Offset Due To Local Raycast? 1 Answer
SInce when?!?!? (Raycast issue) 2 Answers