Hi folks,
I’m trying to do a custom gravity setup to work with characters moving on small planets. Unfortunately I have some trouble with how the transform moves when it’s close to 100% downwards (0,-1,0).
Around that spot, the character starts to turn in circle really quickly.
But when I cross it straightly coming forward from the top I have no problem going through it.
Probably a misunderstanding of quaternions… but I’ve been doodling around for a while now!. A fresh look would help and be super appreciated
Cheers,
Vincent
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GravityPlayerController : MonoBehaviour {
// Movement
private Rigidbody myBody;
private Transform myTransform;
private Vector3 moveDirection;
private Vector3 gravityUp;
private Vector3 worldUp = new Vector3(0, 1, 0);
public float moveSpeed = 1f;
public float gravityForce;
public GravitySource gravitySource;
// Inputs
private float moveFrontBack, moveLeftRight;
private float mouseX, mouseY;
public float mouseSensitivity = 10f;
public Text debugText;
// Other objects
public GameObject target;
void Start()
{
myTransform = transform;
myBody = GetComponent<Rigidbody>();
gravityForce = gravitySource.gravityForce;
}
void Update()
{
mouseX += Input.GetAxis("Mouse X");
gravityUp = (myTransform.position - gravitySource.transform.position).normalized;
moveDirection.Set(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
Debugging();
}
void FixedUpdate()
{
myBody.MovePosition(myTransform.position
+ myTransform.TransformDirection(moveDirection)
* moveSpeed);
myBody.MoveRotation(Quaternion.FromToRotation(worldUp, gravityUp)
* Quaternion.Euler(0, mouseX, 0));
myBody.AddForce(gravityUp * gravityForce);
}
void Debugging()
{
debugText.text = "Rotation = " + myTransform.rotation.ToString("F6") + "
"
+ "Position = " + myTransform.position.ToString(“F6”);
}
}
public class GravitySource : MonoBehaviour {
public float gravityForce = -9.8f;
}