- Home /
[SOLVED] How to set an object's rotation parallel to a wall.
The title says it all. I'm making a wall running mechanic for a third person platformer.
I'm basically trying to make it so when all the parameters are met and the "wallRun" method starts, I want the player to set it's rotation (just the y part) parallel to the wall so it will later translate forwards and not crash into the wall.
However I do not want to simply set the objects rotation equal to the wall's rotation. Later down the line, that won't work for what I'm planning. I need some sort of raycast / Vector3.Cross magic.
Since the player's rotation will vary when the "wallRun" method starts, I do not know how to raycast towards the wall. I do track if the wall is on the right or left side of the player via trigger hitboxes.
If I could raycast towards the closest point on the wall (at the same height) then raycast up, I could Vector3.Cross those and get a forwards vector parallel to the wall.
So basically, does anyone know how to raycast to the closest point on the wall? Or does someone have a better idea than what I'm thinking. (I'm sure someone does :P) Thank you in advance!
EDIT: This issue has been solved! :D Read the comments to see the details.
What do you know about this wall precisely ?
If you have the normal vector of your wall, you can define a ray where the direction is in the opposite direction of the wall's normal vector.
Take a look at this website :
Wolfram $$anonymous$$athWorld : Point-Plane Distance
That is a poorly drawn example. So would that all work? I feel like the code would mess up at the cross part since the pink and green vectors aren't touching. Any ideas?
I don't think it will mess up as you say.
The cross product doesn't $$anonymous$$d if the vectors are touching or not (a direction vector can be placed anywhere)
Have you tried to implement this solution ? Where do you face difficulties ?
Lol I always thought this solution would fail so I didn't even attempt it. face palm I'm busy at the moment but I will implement the method when I get a chance and get back to you. Regardless thanks for your help! I greatly appreciate it good sir! (or ma'am)
O$$anonymous$$. Found time to test it. Lol, wasn't expecting I was gonna have enough time. So all is fine EXCEPT the crossed vector sometimes crosses in the opposite direction. Is it possible to force the vector to always go "forward"? The movementmode means if it is right or left. 1 being right and 2 being left. I tried using a vector offset but that gave me more issues so thats why you see a redudnant section of "+ new Vector3()" I left it in for you. Here's my code:
if (movement$$anonymous$$ode == 1)
{
RaycastHit hit;
if (Physics.Raycast (transform.position + new Vector3(0f,0f,0f),transform.right,out hit,10f))
{
Vector3 temp = Vector3.Cross (transform.up,hit.normal);
transform.rotation = Quaternion.LookRotation(-temp);
}
}
if (movement$$anonymous$$ode == 2)
{
RaycastHit hit;
if (Physics.Raycast (transform.position + new Vector3(0f,0f,0f),-transform.right,out hit,10f))
{
Vector3 temp = Vector3.Cross (transform.up,hit.normal);
transform.rotation = Quaternion.LookRotation(-temp);
}
}
Your answer
Follow this Question
Related Questions
Raycast on LocalCoordinates 1 Answer
Can't figure out how to make my raycast obstacle avoidance less jittery 3 Answers
How to make an object rotate to raycast hit from mouse? Unity 3D 1 Answer
Raycast Rotation on Steering Wheel 0 Answers
Rotate object to make it point towards raycast hit point. 0 Answers