- Home /
Rotating an object based on gravity and camera
Hi guys, I've been struggling away with this problem for far too long, so I'm reaching out for help now. I imagine the answer is fairly simple and there are probably many ways to solve it but I can't get my head around it. I'm also fairly new to all this...
The problem is the following:
I've got an object that is rigidbody cube that contains a child of a mouse look x player controller, then a mouse look y camera child in that, essentially an FPS setup (but not using the standard controller as gravity in that is fixed to only one axis). When the player clicks on a wall it changes gravity to that walls inverse direction (so the player falls towards the wall they click on). When the player lands it rotates the outside rigidbody cube to the same rotation as the wall it is approaching, so that the camera aligns to the walls direction and can move normal. On the whole this approach works, but I get an unfortunate side affect of spinning the cube on the wrong axis (thus spinning the camera in confusing directions) because the cube rotates to the walls rotation.
What I want is for the rotation only to occur in a 'natural way' which is partly a conceptual problem, but basically only rotate around one axis until the up (y axis) of the cube is pointing in the opposite direction to gravity, but since the cube can be changed to be any angle it could be any axis (I think?).
I've tried the following:
transform.rotation = Quaternion.Lerp(transform.rotation,targetUp, Time.deltaTime * speed);
where targetUp is the Quaternion rotation of the wall the player aimed at, it works, but this rotates other axis for obvious reasons. Also doing transform.up to match the walls' .up seems to make it spin in crazy ways too.
I'm not sure whether anyone can help with the information given? Any thoughts and help much appreciated! Can provide lots more info if anyone wants!
Your answer
Follow this Question
Related Questions
AddTorque but don't rotate around centre of mass? 0 Answers
Rotational Gravity - Camera and Force problems 1 Answer
Rotation not working properly(3D) 1 Answer
Trying to walk on a wall 0 Answers
Flip over an object (smooth transition) 3 Answers