- Home /
 
               Question by 
               dagnja · Feb 21 at 03:21 PM · 
                rotationquaternionrotate object  
              
 
              Object rotates on its side on play
Hi, I have been making this game where you drive your car around a planet, I use gravity to attract the car to the planet, but the problem is when I press play my car turns on its side, why is this happening? 
Here is the planet script:
 public class planetScript : MonoBehaviour
 {
    
    public float gravity = -12;
    void Start(){
    GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
   
    }
     
    public void Attract(Transform playerTransform){
        Vector3 gravityUp = (playerTransform.position - transform.position).normalized;
        Vector3 localUp = playerTransform.up;
 
        playerTransform.GetComponent<Rigidbody>().AddForce(gravityUp * gravity);
 
       Quaternion targetRotation = Quaternion.FromToRotation(localUp,gravityUp) *  playerTransform.rotation;
       
        playerTransform.rotation = Quaternion.Slerp(playerTransform.rotation, targetRotation, 10f * Time.deltaTime);
    }
    
 }
And here is the car gravity script:
 public class playerGravity : MonoBehaviour
 {
     public planetScript attractorPlanet;
     private Transform playerTransform;
     void Start()
     {
         GetComponent<Rigidbody>().useGravity = false;
         GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
       
 
         playerTransform = transform;
         
         
     }
 
         void FixedUpdate()
     {
         if(attractorPlanet){
             attractorPlanet.Attract(playerTransform);
         }
     }
 }
 
 
                 
                pitanje1.png 
                (333.1 kB) 
               
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by dagnja · Feb 22 at 10:47 AM
For anyone having the same problem, I have managed to fix the problem by changing the line: Quaternion targetRotation = Quaternion.FromToRotation(localUp,gravityUp) * playerTransform.rotation;
to:
Quaternion targetRotation = Quaternion.LookRotation(gravityUp,Vector3.up);
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                