- Home /
Character won't rotate right in faux gravity enviroment.
So I've made a planet (sphere) and dropped a body (capsule) with rigidbody near it. I managed to recreate a gravity effect via simple vector mathematics and it worked! But I'm at the stage where I'm trying to figure out how to move the capsule around the sphere. My first problem that I ran into - is it's rotation. I managed to do it right when I tried making an FPS controller with default gravity (aka on a simple plane) but everything's acting strange with faux gravity. I try to rotate it (the whole capsule with the camera) along the Y axis - it snaps back to it's original rotation, I try to rotate the camera along the X axis - it works but it ignores the clamp. Can someone explain what might be the problem? Here are the scripts I used (both are on the capsule):
Gravity.cs:
[RequireComponent(typeof(Rigidbody))]
public class Gravity : MonoBehaviour
{
public float gravity = 10.0f;
private Transform planetT;
private void Awake()
{
planetT = GameObject.FindGameObjectWithTag("Planet").GetComponent<Transform>();
}
private void FixedUpdate()
{
Vector3 center = (transform.position - planetT.position).normalized;
GetComponent<Rigidbody>().AddForce(center * gravity);
transform.rotation = Quaternion.FromToRotation(Vector3.up, center);
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
GetComponent<Rigidbody>().useGravity = false;
}
}
FirstPersonController.cs:
public class FirstPersonController : MonoBehaviour {
public float mouseSensitivity = 5.0f;
Transform cameraT;
private void Awake()
{
cameraT = gameObject.GetComponent<Transform>().GetChild(0).GetComponent<Transform>();
}
private void Update()
{
float clampedMouseX = Mathf.Clamp(-Input.GetAxis("Mouse Y") * mouseSensitivity, -90, 90);
transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivity);
cameraT.localRotation *= Quaternion.Euler(clampedMouseX, 0.0f, 0.0f);
}
}
Answer by FortisVenaliter · Jul 26, 2017 at 04:21 PM
You need to use relative rotations. You can't just use Euler when you don't know what the local Y axis is aligned to. Use the Quaternion.AngleAxis with your transform's axes. That should get you in the right direction.
Progress. I redid the code on my FPS controller script like this: cameraT.localRotation *= Quaternion.AngleAxis(clamped$$anonymous$$ouseX, Vector3.right);
. I also took off the gravity script and the controller worked fine aside from the fact that it still ignored the clamp (not sure why). So there's something in gravity script that is disallowing any rotation along the Y axis for the capsule and it's not the rigidbody constraint because the issue persisted even when I removed that line.
Yeah, you're setting the rotation absolutely in the update for the gravity script. That would override any incremental updates.
Also, you need to stop using fixed axes for a spherical body. Ins$$anonymous$$d of Vector3.right, use transform.up, etc.
Plopped the constraints and useGravity lines in Awake() method - no change. And changing Vector3 to transform don't do much good too...
Your answer
![](https://koobas.hobune.stream/wayback/20220612131232im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Engine weight Simulation 2 Answers
Rotating sphere with rigidbody attached 1 Answer
How can I rotate and move a cylindrical rod from one side with respect to other side? 1 Answer
why can't my Rigidbody not rotate on slopes? 1 Answer
Limit Rotation of Rigidbody to 45 Degrees (Handlebars on a Bicycle) 1 Answer