- Home /
Raycast distance affected by momentum of character
Hello all,
basically I'm working on a fighting game, and I'm using a raycast from the player position forward at given punch distance to test for a hit. I'm using controller.SimpleMove for movement. Everything seems to work right if I'm standing still, but if I'm walking toward the other character, punches will land from far, far away (and even then the hit.distance is extremely low). Here's the relevant code:
function ApplyPunch(punchType : punch)
{
var hit : RaycastHit;
var fwd = transform.TransformDirection (Vector3.forward);
var pos = transform.position;
var distance = punchType.distance;
if (Physics.Raycast (pos, fwd, hit, distance)) {
etc....
If I'm standing right in front of the other guy, hit.distance is about .5, but if I'm moving toward him, even from all the way across the scene, it's about .02. So I guess the question is, how can I get my raycast to ignore the momentum of my character? None of the variables being passed in (as shown above) change when I'm moving, so I just can't figure out why this is happening. Thanks in advance to anyone with suggestions..
Answer by goranzoricic · Feb 16, 2012 at 10:45 AM
Are you sure that your ray cast is hitting the other guy, and not the character that is shooting it? Such a short distance would suggest something like that. You should make sure that the ray cast ignores the originating character. Perhaps something like this:
http://forum.unity3d.com/threads/37900-Raycast-quot-Ignore-quot-Layers
Try adding (inside the if(raycast)) Debug.Log(hit.transform.name)
or tossing in a Debug.DrawRay
to show the raycast.
Raycasts don't use momentum. Something else about movement is messing you up -- maybe running angles your head forward, so the old raycast came from inside your head (so ignored it) and the new one hits the back of your own head.
Hmm, yep, thanks to you both, can't believe I missed that. I'm using if(hit.collider.gameObject == otherplayer), to double-check, but I had a bracket in the wrong place, so it was only checking on blocked punches... $$anonymous$$oved the bracket to include hits, problem solved. Thanks!
Now if only I could figure out how to accept your answer on this page...
Your answer
Follow this Question
Related Questions
Unreliability of Physics.Raycast.Distance for player movement? 0 Answers
Raycasts left/right problem. One works, other doesn't...? 1 Answer
How to get the distance between two objects in feet/meter? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Slant Physics Raycast Implementation 0 Answers