- Home /
Raycasts left/right problem. One works, other doesn't...?
Hello again. I'm back with yet another problem. I have a good move script that is working pretty well, using Translate, Raycasts, and a rigidbody for some other things. It works great! Except for one problem with the Raycasts.
I'm telling it to check left and right every frame to see if there's a collider right next to the ship and if so, the ship's speed = 0, else it equals 15 (the normal speed). So anyway, the problem is this.
See this video I made:
The only part of my script that matters:
var lft = transform.TransformDirection (Vector3.left);
var rgt = transform.TransformDirection (Vector3.right);
if (Physics.Raycast (transform.position, lft, 1.8))
{
speed = 0;
}else
{
speed = 15;
}
if (Physics.Raycast (transform.position, rgt, 1.8))
{
speed = 0;
}else
{
speed = 15;
}
}
If I take off either the LEFT raycast or the RIGHT raycast, the one that is actually in there works fine, as seen in the video. If they're both in there together (as they are above in my code) then one works and the other doesn't. It just glitches like in the video.
Anyone got any ideas? Surely I'm doing something wrong, but it's weird that one of them works perfectly fine and the other doesn't, when they are the EXACT same code, just reversed…
Thanks!
Answer by by0log1c · Mar 21, 2012 at 03:04 PM
Just pointing out that both raycast are fighting one another. Whatever the first one does will be overwritten by the second. Try putting both Raycast in the same if() condition, IE:
if(Raycast1() || Raycast2())
{
DoStuff();
}
else
{
DoOtherStuff();
}
And that's exactly the problem. No matter what the first raycast is doing, only the second one will be effective. The second one decides if the speed is 0 or 15.
ps. @CaptainIcy: just out of curiosity, is there a reason why you don't want to use colliders / physics? The CharacterController or a Rigidbody will handle collisions automatically.
You know what? That's exactly what was wrong. I should've realized. I had a reason I wasn't doing it that way, because before I started setting the horizontal speed to 0, I was telling it to Translate it the opposite way a certain speed, so that it would continually just bounce back. So it using this method wouldn't have worked with that. It's working now. Thanks. :P
Also, I want the kind of control I can only get using Translate. Rigidbodies are a little too hard to deal with, and it's physics-based. I don't need physics in my game. Just simple colliding. And as for the character controller, it would work fine, except it uses a capsule collider, and for my ship, that just wasn't going to work. I spent a week trying to get it to work and finally realized it wasn't feasible with a capsule collider handling the gravity/collisions. I needed a precise box collider to make it work correctly.
Thanks for the help. I guess I forgot to turn on my brain today. :p
Your answer
Follow this Question
Related Questions
Questions about movement on uneven/slopped terrain and terrain coliders 0 Answers
Raycasting on Moving Colliders 0 Answers
Move player to specific location and avoid obstacle. 2 Answers
Moving objects using raycasting? 1 Answer
Making object found with raycast a child of the object the ray is cast from 1 Answer