- Home /
Get raycast to ignore rotational axis
In my sidescroller game the character's Z rotation is based on a raycast pointing from -transform.up allowing him to rotate along convex/concave surfaces (and walk up walls and along ceilings). I'm creating my own gravity that always pulls the character towards any surface that's below him (locally) and the character rotation is always based on the normal of the surface the raycast is hitting.
This works great, however, one of the key mechanics for my game will be to allow the player to flip up/down from ceiling/floor or flip left/right from right wall/left wall.
When I rotate the X axis to make the character flip to another surface the raycast will angle diagonally off the terrain causing some wild spinning and rotations.
My question boils down to this:
Is it possible to have the raycast not rotate with the player's X rotation? Just the Z?
Hope I explained this well. I new to Unity. Loving it so far!
Thanks!
Answer by ByteSheep · Mar 20, 2012 at 04:08 PM
Since you're probaly using -transform.up on the character, the raycast will always be facing downwards according to the character.. My first thought would be to make the character a child object of a new empty object and give the new object the raycast instead. That way the character can flip etc without affecting the raycast, but the raycast position will still follow the character (if you move the new object instead of the character). Another idea would be to disable the ray while the character is flipping, as to avoid the strange spinning ;)
I originally tried that, but I would still get wacky results... However, after you mentioned this, I revisited that as a solution and what I came up with worked.
$$anonymous$$y original, similar solution, was instantly changing the rotation of the player so the raycast would flip and detect the opposite walls/floors. Then, I would change the child character model's rotation accordingly. However, this did not work; still flip flopping all over the place.
Solution: ins$$anonymous$$d of rotating the player I made a variable called "flipDir" that's either 1 or -1 and then multipled it to the raycast direction, gravity, and jump direction when the flip button is pressed.
Now, I have a new problem... Not sure how to do a smooth local rotation of the child character model within player parent. Seems to always be global rotation. However, no matter what funky rotation I get, at least the parent character model is rotating and behaving properly now.
Thanks!
var newRot = transform.localRotation * Quaternion.Euler(180,0,0); chara.transform.rotation = Quaternion.Slerp(chara.transform.rotation, newRot, Time.deltaTime * 2);
That fixed the rotation.
I'm confused about something though. How come this code:
var newRot = transform.localRotation * Quaternion.Euler(180,0,0);
Is not the same as this code:
var newRot = Quaternion.Euler(180,0,0) * transform.localRotation;
Answer by rutter · Mar 20, 2012 at 06:39 PM
After grabbing -transform.up
, you could zero out a particular axis before building your raycast.
For example:
var dir = -transform.up;
dir.z = 0f;
var ray = new Ray(transform.position, dir);
That would work too, but I think the first answer will give me better control of my character animations and the collision of the parent player object.
Your answer
Follow this Question
Related Questions
How to keep local rotation when using raycasts? 1 Answer
Flip over an object (smooth transition) 3 Answers
How to get Ray rotation from direction 0 Answers
Detecting 360 degree spin on local y axis 2 Answers
Detect if on ground? 1 Answer