- Home /
Y rotation of object based on center of a sphere?
Hi, I have a player object that can walk around a sphere (kind of like a mini planet). I'm using a Rigidbody on the player and addforce that is constantly pointing towards the sphere's center to simulate gravity. What I can't figure out is how to keep the player's Y rotation fixed so that it appears to be walking on the surface. The code I am using now uses raycasting and Quaternion.FromToRotation(Vector3.up, hit.normal) to fix it to the normals of the sphere, which works well most of the time... BUT, when it gets to the bottom of the sphere (Vector3.down in world space), things get really wonky. I'm thinking that using the Gravity vector (which always points to the sphere's center), would be a great way to fix this, I just can't wrap my head around how to do it. Thanks for answering! Here is the code:
void FixedUpdate()
{
_h = Input.GetAxis("Horizontal");
_v = Input.GetAxis("Vertical");
//direction of movement defined by player input.
_moveDirection = new Vector3(_h,0,_v).normalized;
//this vector always points to the sphere center and is added to the player's rigidbody for gravity effect.
_sphereBasedGravity = (_sphere.transform.position - transform.position).normalized;
//add force to player based on input.
_thisRB.AddRelativeForce(_moveDirection * _moveSpeed);
//adds gravity based on sphere center.
_thisRB.AddForce(_sphereBasedGravity * _gravityMultiplier);
}
void Update ()
{
//set up ray that goes to ground debugging and variables.
_rayToGroundStartPos = _base.position;
Debug.DrawRay(_rayToGroundStartPos, _sphereBasedGravity * 4, Color.green);
if (Physics.Raycast(_rayToGroundStartPos, _sphereBasedGravity * 4, out hit))
{
if ( hit.collider.tag == "Ground")
{
_isGrounded = true;
}
else
{
_isGrounded = false;
}
}
//player rotation (to align with sphere).
Quaternion _toRot = Quaternion.FromToRotation(Vector3.up, hit.normal);
transform.rotation = _toRot;
}
Your answer
Follow this Question
Related Questions
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 on LocalCoordinates 1 Answer
Generate mesh from raycast positions, independent of rotations 0 Answers
Rotate object to make it point towards raycast hit point. 0 Answers