I am currently using a system for faux gravity demonstrated by Sebastian Lague, using an object as a gravity attractor and one as a body then simply aligning the body with the attractor and applying force towards it. See Below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GravityAttractor : MonoBehaviour {
public float gravity = -10f;
public void Attract(Rigidbody body)
{
Vector3 gravityUp = (body.position - transform.position).normalized;
Vector3 localUp = body.transform.up;
// Apply downwards gravity to body
body.AddForce(gravityUp * gravity);
// Allign bodies up axis with the centre of planet
body.rotation = Quaternion.FromToRotation(localUp, gravityUp) * body.rotation;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GravityBody : MonoBehaviour {
public GravityAttractor attractor;
private Rigidbody rigidbody;
void Awake()
{
// planet = GameObject.FindGameObjectWithTag("Planet").GetComponent<GravityAttractor>();
rigidbody = GetComponent<Rigidbody>();
// Disable rigidbody gravity and rotation as this is simulated in GravityAttractor script
rigidbody.useGravity = false;
rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
}
void FixedUpdate()
{
// Allow this body to be influenced by planet's gravity
attractor.Attract(rigidbody);
}
}
I desired to have my player look at the mouse and then move towards the direction when pressing a key. I have little experience with Quaternions and i believe this is where the main faults in my code lie. When using the rotation code i experience jittering of the player up and down. I have ruled out mesh inconsistencies and also have ruled out collider interference, i do however believe it is due to the rapid changes of transform.rotation
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float moveSpeed = 15;
public float rotateSpeed = 5;
private Vector3 movementInput, movement, lookTarget;
private Rigidbody rigidbody;
private Quaternion rotationTarget;
private bool forward = false;
void Start()
{
rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
// movementInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized;
// movement = movementInput * moveSpeed * Time.deltaTime;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
lookTarget = hit.point - transform.position;
rotationTarget = Quaternion.LookRotation(lookTarget, transform.up);
transform.rotation = Quaternion.Slerp(transform.rotation, rotationTarget, Time.deltaTime * rotateSpeed);
}
// Debug.DrawRay(transform.position, transform.forward * 5);
}
private void FixedUpdate()
{
if (Input.GetKey(KeyCode.W))
rigidbody.MovePosition(rigidbody.position + transform.forward * moveSpeed * Time.fixedDeltaTime);
if (Input.GetKey(KeyCode.S))
rigidbody.MovePosition(rigidbody.position - transform.forward * moveSpeed * Time.fixedDeltaTime);
}
}
The jittering happens due to the following lines of code:
lookTarget = hit.point - transform.position;
rotationTarget = Quaternion.LookRotation(lookTarget, transform.up);
transform.rotation = Quaternion.Slerp(transform.rotation, rotationTarget, Time.deltaTime * rotateSpeed);
Any help on how to solve this problem would be appreciated, Thanks! ![]()