- Home /
Question by
Zedock · Dec 16, 2016 at 11:51 AM ·
unity 53dgravityhingejointfauxgravity
Faux Gravity help
I have recently been experimenting with spherical planet gravity (faux gravity), similar to what exists in Super Mario Galaxy. However, as my player rotates around the planet, it slowly moves further and further away - (https://gfycat.com/BitesizedWellgroomedGrassspider).
Why is this occuring? Below I have included both scripts I use. One is for player movement and the other is for gravity. Does it relate to my gravity? Or my player movement not being compatible with my gravity? Any suggestions?
GRAVITY
using UnityEngine;
using System.Collections;
public class PlayerGravity : MonoBehaviour
{
#region Variables
//Private var
public GameObject currentPlanet;
private float damping = 8;
//Public var
public GameObject groundCheck;
public LayerMask planet;
public float groundCheckRadius;
public float gravity;
#endregion
void FixedUpdate ()
{
//Direction from player to planet
Vector3 dir = (transform.position - currentPlanet.transform.position).normalized;//CheckForGroundContact().transform.position );
//Position of player
Vector3 origin = transform.position;
//Direction upwards;
transform.up = Vector3.Slerp(transform.up, dir, Time.deltaTime * damping);
}
}
PLAYER MOVEMENT
using UnityEngine;
using System.Collections;
public class PlayerBehaviour : MonoBehaviour
{
void Update ()
{
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
transform.Translate(move * 10 * Time.deltaTime);
}
}
Comment
You can do playerRigidbody.AddForce(transform.position - currentPlanet.transform.position).normalized*gravity;
I did not check it, but it should work. You can put it in the player script in like Update();