- Home /
Rotate object without rotating RayCast
Is it possible to rotate an object that has RayCast collision attached to it WITHOUT rotating the RayCast itself on that specific object?
When I run my game, this is how it looks like:
Red dot = Player, looking UP
Blue stripe = RayCast in front
Orange stripe = RayCast to the right
Green stripe = RayCast to the left
Yellow stripe = RayCast in the back
So, whenever I press (for example) Arrow Right it should rotate the OBJECT 90 degrees but the RayCast should not rotate. Like this:
Red dot = Player, looking to the RIGHT
Stripes remain the same
I can't find anything on the internet that helps me with not rotating RayCast upon object rotation. ANYONE?
Answer by Anne_Marije · Mar 07, 2012 at 12:15 PM
The raycast isn't actually rotated with the object. The vector.forwards you are using to cast it is rotated with the object.
You could initialize your own vectors at start and use those instead.
For example in C#
Vector3 North;
Vector3 South;
Vector3 West;
Vector3 East;
void Start() //assuming player starts facing north
{
North = Vector3.forward;
South = new Vector(0,0,-1)
West = new Vector(-1,0,0)
East = Vector.right;
}
checkColisionFront(North); //kind of weird methode name to be honest as this won't be the front of your gameobject anymore after rotation.
The vectors may need some tweaking, I haven't tried it. Hopefully this helps you.
I get your point, and I applied it to my current solution. Works like a charm, thank you. Now my next issue is to rotate the object itself via the eulerAngles. Thanks again!
Answer by Owen-Reynolds · Mar 05, 2012 at 03:39 PM
The direction of the raycast is entirely under your control. There's a slot where you say where it starts and what direction it goes. Fill them in with whatever you want.
If you use transform.forward
then you are telling Unity to go forward based on your rotation. That's what the transform
in front specially does. Likewise, if you raycast from something childed to you, you're telling Unity that the start of the raycast should rotate with you (that's what childing is for.) All you have to do is figure out what means actual North or East... . Turns out that's just Vector3.forward
or Vector3.right
.
Answer by KruegerT · Mar 05, 2012 at 03:41 PM
I don't know whether i get your question right, but you could probably just use the global direction vectors? E.g. Vector3.right, will always point in the same direction.
In your picture the player's forward-vector after the rotation is the same as the world's right-vector.
Greetings, Thomas
Answer by sjefke31 · Mar 06, 2012 at 08:47 AM
@Owen Reynolds, I am using the following code, this rotates my RayCast upon gameobject rotation.
checkCollisionFront = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position + new Vector3(0.15f, 0.1f, 0), checkCollisionFront, out hit, distance))
{
//Do Something Here
}
Anything I should do differently?
transform.TransformDirection
specifically tells Unity you want to take NORTH (which is what Vector3.forward
is) and twist it to the way you are facing. You don't have to twist it like that if you actually want north.
Thank you for your answer, but actually I don't really understand what you mean. With the method you just explained, how exactly can I make the RayCast always point a given direction? Even when the object where it's attached to rotates to a different direction.
So again, I have 4 RayCasts: Every RayCast points to a different direction. The RayCast is attached to an object, if the object rotates it shouldn't rotate the RayCast with it.