Rotating Two Axes for Gravity,Rotating only two axes towards Object
I am trying to simulate simple Gravity by code. I have already written a piece that determins the closest "Planet" and stores its GameObject in a Variable ( "nearestPlanet"
). I am trying to implement the Character (currently a cube) to turn towards the Planet and Add a Force towards it (Gravity :D) .
My current Solution works like this:
for making the Turn:
void GravityTurn()
{
Vector3 targetDir = nearestPlanet.GetComponent<Transform>().position - tr.position;
float step = gravityspeed * Time.delta,I am trying to simulate simple Gravity by code. I have already written a piece that determins the closest "Planet" and stores its GameObject in a Variable ( ` "nearestPlanet" ` ).
I am trying to implement the Character (currently a cube) to turn towards the Planet and Add a Force towards it (Gravity :D) .
My current Solution works like this:
for making the Turn:
void GravityTurn()
{
Vector3 targetDir = nearestPlanet.GetComponent<Transform>().position - tr.position;
float step = gravityspeed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(tr.forward, targetDir, step, 0.0f);
tr.rotation = Quaternion.LookRotation(newDir);
}
and for Pushing him down:
void GravityPush()
{
rb.AddRelativeForce(0, 0, 5f);
}
The Gravity turn is just the Stock code from Unity to "Rotate towards".
Current Solution does not allow rotation in y axis (no mouse movement) and gets glitchy near to the top and bottom of planets. For it to work, the Object has to be rotated 90° so it doesn't face face down but bottom down
Is there a possibility to just instantly turn those two axes towards the nearest planet and leave the other axes alone. There doesnt need to be a smooth rotation
Movement of Mouse:
tr.Rotate(0, 0, Input.GetAxis("Mouse X")*-1);
I hope I could describe the Problem (My first Question)