Hello all, i am experimenting around with some game mechanics involving gravity rotating around a circular planet. All has been going well so far, mostly. However, i noticed a slight bit of unwanted inertia on my player object, that is, i wasn’t inputting any movement but it was still sliding ever so slightly. My normal trick for this would be to set the player’s x velocity to 0 every frame they aren’t inputting a horizontal direction, however i quickly realized that this caused problems with the planetary gravity mechanic. The player has a capsule collider 2d and the planet is a combination of a gameobject with a circle collider as the parent of an object with a trigger collider and a script named GravPoint.cs.
Holding left and jumping:
Standing still and jumping:
I need to find a way to make the Player’s sense of right and left Relative to it’s orbit on the planet so i can stop it from sliding around when not in movement.
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D MyRigidBody;
private float horizontalInput;
public float movespeed;
public float jumpForce;
// Start is called before the first frame update
void Start()
{
MyRigidBody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
//Jumping code
if (Input.GetKeyDown(KeyCode.Space))
{
MyRigidBody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
}
}
private void FixedUpdate()
{
MyRigidBody.AddForce(transform.right * horizontalInput * movespeed);
//This is the problem
if(horizontalInput == 0)
{
Vector3 veloc = MyRigidBody.velocity;
veloc.x = 0;
MyRigidBody.velocity = veloc;
}
}
}
public class GravPoint : MonoBehaviour
{
public float GravityScale;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerStay2D(Collider2D collision)
{
if(collision.GetComponent<Rigidbody2D>() != null && !collision.CompareTag("UnaffectedByOrbit"))
{
Vector3 Direction = (transform.position - collision.transform.position) * GravityScale;
collision.GetComponent<Rigidbody2D>().AddForce(Direction);
collision.transform.up = (collision.transform.position - transform.position);
collision.GetComponent<Rigidbody2D>().drag = 1.5f;
}
}
}