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:

alt text

  • 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:

alt text

  • 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?

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.

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

@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?

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.