I'm working in a project with Unity 2d where player walks on a small planet. I try to use some code from youtube tutorial to apply gravity to my planet.This code consists of two parts, Attractable and Attractor. Attractables are used on objects that are drawn and Attractors placed on planets. I use simple platformer move script to move my player. But I found that if my player moves in one direction and suddenly changes the other direction, my player can "fly". I think this happened because my players moved at the wrong angle. I use my Attractor script to constantly applying force towards the center of the planet every update, but my player still fly and doesn't get pulled down unless if i run to another dirrection, then my player will get pulled again. I'm confused because my player is supposed to be pulled every update, but it seems like it's not being pulled.
in Attractor script, it just find all object inside Circle area with Physics2D.OverlapCircleAll
and then call Attract function from object with Attractable script. The Attractor code look like this:
void Update()
{
SetAttractedObjects();
}
void FixedUpdate()
{
AttractObjects();
}
void SetAttractedObjects()
{
AttractedObjects = Physics2D.OverlapCircleAll(planetTransform.position, effectionRadius, AttractionLayer).ToList();// can be optimized
}
void AttractObjects()
{
for (int i = 0; i < AttractedObjects.Count; i++)
{
AttractedObjects[i].GetComponent<Attractable>().Attract(this);
}
}
And inside the Attractable script, it has Attract function, that apply some force to object rigidbody. This is the function called from the previous Attractor script.
public void Attract(Attractor artgra)
{
Vector2 attractionDir = (Vector2)artgra.planetTransform.position - m_rigdibody.position;
m_rigdibody.AddForce(attractionDir.normalized * -artgra.gravity * 100 * Time.fixedDeltaTime);
if (currentAttractor == null)
{
currentAttractor = artgra;
}
}
This script also has, a RotateToCenter function which ensures the player is always facing the planet. This function is called from the Update function.
void RotateToCenter()
{
Vector2 distanceVector = (Vector2)currentAttractor.planetTransform.position - (Vector2)m_transform.position;
float angle = Mathf.Atan2(distanceVector.y, distanceVector.x) * Mathf.Rad2Deg;
m_transform.rotation = Quaternion.AngleAxis(angle + 90,Vector3.forward);
}
And i use this movement code that add some velocity to my rigidbody to move my player;
void Update( )
{
Move();
}
void Move(){
scale=transform.localScale;
if (Input.GetKey(KeyCode.A))
{
isMoving=true;
rb.velocity = transform.right * -moveSpeed;
scale.x=-1;
}
else if (Input.GetKey(KeyCode.D))
{
isMoving=true;
rb.velocity = transform.right * moveSpeed;
scale.x=1;
}
else
{
rb.velocity = new Vector2(0f, 0f);
isMoving=false;
}
transform.localScale=scale;
animator.SetBool("isRunning",isMoving);
}
I think the problem is in the Attract function because my player doesn't seem to be pulled to the center of the planet, but when I try to debug it, it seems that this function is still being called every update, even when my player is hovering in the air. Anyone know how to solve this?