- Home /
Raycast is being unreliable
Hi there,
I have a script that i use to Raycast to an object then send a message to it to execute a function. It works, the problem being that it is very unreliable and seems to act on a kind of delay.
For example, i will approach my enemy, looking directly at him and it is only when I go around to the back of his model and stare at it that the Raycast hits him, sends the message and then executes the desired function. Here is both the Raycast script and the script attached to the enemy.
var hit : RaycastHit;
var TheDammage : int = 100;
function Update()
{
if ( Physics.Raycast( transform.position, transform.forward, hit, 3 ))
{
Debug.Log( "ray hit (tag) : " + hit.collider.gameObject.tag + " : " );
if ( hit.collider.gameObject.tag == "EnemyStatic" )
{
Debug.Log("HitStaticEnemy");
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
}
pragma strict
@script RequireComponent(AudioSource)
var Soundplayed1 = false; var HorrorHit1: AudioSource; var Health = 100;
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
if(Health <= 0)
if(Soundplayed1 == false)
{
Dead();
}
}
function Dead()
{
GetComponentInChildren(MeshRenderer).enabled = false;
Debug.Log("MeshHit");
audio.volume = 1.0;
HorrorHit1.Play();
Soundplayed1 = true;
Destroy (gameObject, 5);
}
Also I am aware of the delay in destroying the gameobject. This is intentional as I wanted to allow the audio clip to finish before removing the audiosource.
Are you sure its acting on a delay? From your description, it may only be hitting the character from the back.
Also, as just a quick test, try moving your raycast code to FixedUpdate ins$$anonymous$$d of just Update. FixedUpdate is called more often.
O$$anonymous$$ i changed from Update () to FixedUpdate () no change really.
I'm getting debug log readouts that the ray is hitting untagged colliders (terrain) then after staring at the back of my enemy model for about 5 seconds, the desired function is executed. I am really stumped with this one. But I do think it is a problem within the first script. because as soon as the raycast hits the enemy, it calls the desired function through Sendmessage.
Try using the Debug.DrawRay()
function to see what is your ray hitting exactly. The line will be drawn in scene view.
On which game object is the first script attached? On FP camera, player mesh?
Correction on 2d4Games: FixedUpdate is called more often ONLY if the frame rate is lower than the fixed delta time. Not always.
Answer by Bunny83 · Aug 17, 2013 at 03:53 AM
Try this:
// [...]
function GetHierarchyName(obj : Transform) : String
{
if (obj == null)
return "ROOT";
return GetHierarchyName(obj.parent) + "/" + obj.name;
}
function Update()
{
if ( Physics.Raycast( transform.position, transform.forward, hit, 50 ))
{
Debug.Log("We hit: " + GetHierarchyName(hit.collider.transform));
Debug.DrawLine(transform.position, hit.point, Color.yellow, 0.5);
}
}
this will print the full path in the hierarchy of the object your ray hit. There will also be a line from your ray starting point to the actual hit point. This line will last half a second just to see where the ray actually hits.
ps: you should undock either the scene or game view or dock them next to each other so you can see both at the same time. It's important that you move the scene view camera to a spot where you have a good view of what happens.
There's not much we can do about your problems since we can be sure that the raycast works perfectly and without any delay.
pps: Is it possible that the variable "TheDammage" has a different value in the inspector? The inspector holds the actual value so it doesn't need to be 100 as you might expect.
Thank you so much!
Using your debug script I managed to find out that my raycast was hitting a plane in front of the camera which i use for a camera effect, which is weird because i had disabled the collider on it.
Now by adding the plane to the ignore raycast layer. It all works perfectly!
Thank you very much all of you for your help and especially Bunny83.
Your answer
Follow this Question
Related Questions
Physics.CapsuleCast Help 1 Answer
Raycast stops working after ~40 seconds when target has Rigidbody 2 Answers
Unable to set collision layer from scrip 0 Answers
Realistic Helicopter AI Movements using physics 0 Answers
Make a rolling ball always on ground without falling when reaching the edges of the map 1 Answer