- Home /
Inaccurate Raycasting
NOTE:: This only occurs AFTER moving the camera.... So I'm guessing it has something to do with SmoothFollow
I've uploaded a web-player version so you can see what's going on, please note everything in here is basic and the GUI will be mal-scaled because I haven't worked on scaling anything yet. Either way, here it is in a nutshell....
The raycasting seems to come from the camera, although everytime I use it, it starts at a location lower then the last, which in term is making the raycast very inaccurate. The camera has a smooth-follow script attached to the player, if this could be causing the problem?
Hit Y and Z to spawn a new NPC (They have different names, only visible in Console currently)
Will spawn a capsule, just look for it, each NPC will be +10z away from eachother
Click to target, healthbar will appear to right if targetting
Note: You CAN target it, you just have to click all around it numerous friken times... Http://LP-Gaming.com/WebThing.html
Raycasting code used
if (Input.GetMouseButtonUp(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray, out hit, 250))
{
if(hit.transform.tag == "Untagged") return;
if (hit.transform.tag != "terrain")
{
target = hit.transform;
Debug.Log ("Hit something: " +hit.transform.tag);
}
} else {
target = null;
}
}
EDIT: While debugging further I've noticed that even while clicking on the NPC, 70% of the time the Raycast hits the terrain, not the NPC... I'm not sure what would be causing this, is it going through the NPC?
Edit2: using Debug.DrawLine I noticed that there were two ray-cast lines being drawn, one hitting where it should, and another coming from somewhere unknown (Not the camera, although below it) hitting slightly before it should, scaled down at a lower angle... So, I believe I found my brother, now I just need to find a fix.
EDIT3: It seems that if I HOLD the raycast button down for around 20seconds the secondary Raycast will gradually fall below the terrain and not interact with anything anymore, although this still worries me as to why there is a secondary Raycast.
Add more Debug.Log calls. Does the raycast hit anything at all? Log the tag immediately inside the if.
It's hard to believe it's "stupidly inaccurate", because it's made to work, and it usually just works.. what if you put a debug.log outside the raycasting, to see if the mouse up actually happens there. Also what about the rest of Update? $$anonymous$$aybe you return from the method prematurely somewhere.
While doing this I noticed that even while clicking on the NPC half the time, about 80% of the time it sent me this message
"Raycast hit something with tag: Terrain"
and then the other 20%
"Raycast hit somethign with tag: $$anonymous$$ob" <-- NPC
So, the raycast is going through the $$anonymous$$ob i guess?
Debug.DrawLine or LineRenderer is also a good way of visually displaying a ray cast to see what is actually happening. Just use the camera position and the hit point.
Alright, this is some strange stuff.. Using the Debug.DrawLine I found two lines being drawn, one I couldn't figure out where it was co$$anonymous$$g from, and the other from the camera.. One of the was hitting slightly before the other...
EDIT: It's co$$anonymous$$g from under the camera, almost at terrain level.
Answer by Bunny83 · Jan 24, 2013 at 12:13 AM
It feels like you're doing this in FixedUpdate. Could that be? I'm too lazy to download and decompile :D
Never do such things in FixedUpdate. FixedUpdate is for physics calculations. You can't process any event information there because events are evaluated once per frame. If your visual framerate is greater than the fixedUpdate rate it's pure luck when a MouseDown event happens in exact the same frame where a FixedUpdate is called.
I've made a visualisation of how FixedUpdate works some time ago. Just play with the "framerate" setting and see when Update is called and when FixedUpdate is called.
Thanks, but this is not the case as you can see below
public void Update () {
//Debug.Log (attackTimer);
if(attackTimer > 0) {
attackTimer -= Time.deltaTime;
}
if(Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.O)) {
i++;
Debug.Log ("Pressed O : " + i + " Times");
}
if(Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.$$anonymous$$)) {
if(target != null) {
if(target.tag == "$$anonymous$$ob") {
NPCs.GetInstance().npcList[target.gameObject].currentHealth -= 10;
Debug.Log ("Target's current Health = " +NPCs.GetInstance().npcList[target.gameObject].currentHealth);
}
}
}
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Y)) {
NPCs.GetInstance().createNPC("Chris", 100, 50, 10, new Vector3(130.0f,2.5f,Z));
Z+= 4.0f;
}
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Z)) {
NPCs.GetInstance().createNPC("$$anonymous$$yle", 100, 50, 10, new Vector3(130.0f,2.5f,Z));
Z+= 4.0f;
}
if (Input.Get$$anonymous$$ouseButtonUp(0))
{
i++;
Debug.Log ("Clicked " + i + " Times");
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray, out hit, 100))
{
Debug.Log ("Hit something: " +hit.transform.tag);
Debug.DrawLine(Camera.main.transform.position, hit.point);
if(hit.transform.tag == "Untagged") return;
if (hit.transform.tag != "terrain")
{
target = hit.transform;
Debug.Log ("Hit something: " +hit.transform.tag);
}
} else {
target = null;
}
}
Do you get the "Clicked ..." message reliably? Because when i tested it yesterday deseleting was unreliable as well.
Are you sure you have only one camera tagged $$anonymous$$ainCamers?
Yes, the project is very basic. There's a terrain, a player, and a camera... That's all.
and yes, I was clicking the Clicked message every time.
Your answer
Follow this Question
Related Questions
Unity Gllitching Everything? 1 Answer
Unity completely glitching out 0 Answers
A node in a childnode? 1 Answer
Unity game frame rate really slow. 1 Answer
Objects cloning on movement? 1 Answer