How do i prevent my character from spinning uncontrollably on a planet?
I am trying to make a space simulation where you can walk around planets and travel to other planets. I am very new to programming by the way. I am/was following Sebastian leagues video for the physics and scripts for the player, but when i land on my planet the player just rotates uncontrollably when i move. (Video: https://www.youtube.com/watch?v=7axImc1sxa0&t=629s)I can look around and rotate on the y axis. I modified Sebastians script so that it would fit with my project. Here is my Gravity script:
// Update is called once per frame
void Update()
{
foreach (GameObject planet in planets)
{
m2 = planet.GetComponent<Rigidbody>().mass;
float sqrDst = (planet.transform.position - rb.position).sqrMagnitude;
Vector3 forceDir = (planet.transform.position - rb.position).normalized;
acceleration = forceDir * G * m2 / sqrDst;
rb.AddForce(acceleration, ForceMode.Acceleration);
StrongestGravity = acceleration;
}
Vector3 GravityUp = -StrongestGravity.normalized;
rb.rotation = Quaternion.FromToRotation(transform.up, GravityUp) * rb.rotation;
}
}
And Here is my Movement script:
{ if (Input.GetKey("w")) { rb.AddForce(transform.forward speed Time.deltaTime); }
if (Input.GetKey("s"))
{
rb.AddForce(-transform.forward * speed * Time.deltaTime);
}
if (Input.GetKey("a"))
{
rb.AddForce(-transform.right * speed * Time.deltaTime);
}
if (Input.GetKey("d"))
{
rb.AddForce(transform.right * speed * Time.deltaTime);
}
if (Physics.Raycast(transform.position, -transform.up, out hit, MaxCheckDist))
{
Grounded = true;
}
else
{
Grounded = false;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613024818im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Virtual accelerometer/gyro 0 Answers
player glitching through floor when turning 1 Answer
Why does my player bounce when hitting edge of platform 0 Answers