- Home /
Rotate player towards the floor
Hey guys! I need a script that will rotate the player towards the floor angle. I have been googling a lot, but I could not find anything I could use.
Ok, so what I need to do is rotate the player from
to
The red arrows are indicating the direction the player will move.
Is there any way to do this?
David
Answer by AlucardJay · Apr 13, 2012 at 07:37 PM
Try raycasting down, then reading the 'normal' of the floor. Then rotate the player to match the 'normal' s rotation to the floor. I had the same problem , but luckily Aldo Naletto provided the solution.
My question : http://answers.unity3d.com/questions/230216/why-am-i-getting-a-fruity-effect-from-my-raycast.html
Aldo's other post (with illustrations from the question's asker similar to yours) : http://answers.unity3d.com/questions/168097/orient-vehicle-to-ground-normal.html
So you have both js and C# versions, hope this helps a little =]
example build of my result : http://www.alucardj.net16.net/unityquestions/HoverBoard6.html
Thanks! Thats exactly what I've been looking for :)!!
Answer by JayMHelpsU · Apr 13, 2012 at 07:24 PM
yes there is and its quite simple just get the floors rotation and make the cube as it rotation like so (Javascript)
var Cube : Transform;
var Floor : Transform;
function Update()
{
Cube.rotation = Floor.rotation;
}
drag (in editor) the objects into their designated spots
Do you really think I couldn't have done this? That picture up there is just to show what I mean. I will have different curved floors in my game that won't work with this code.
Im sorry it is quite hard to give help with the litte information provided, i wasnt believing you couldnt have done this , it was just a misunderstanding. Anyway a cube is a complex object to use because its flat sides. Two other options. You can make it go through the floor or you can have it with only various parts of a flat side touching the ground. Sorry for any inconvenience :)
@dzebna, I'm also looking for a solution to this as this is something that I'm also trying to implement in my project. I have a round space station (2001: A Space Odyssey) and I need to have the player rotate to the floor angle as they walk around in it. Were you able to resolve it?
e.g.
var rayHit : RaycastHit;
if (Physics.Raycast(transform.position, Vector3.down, rayHit, 5.0)) {
transform.rotation = Quaternion.LookRotation(rayHit.normal);
}