I want to create a black hole like gravity pull effect in Unity2D, and used that code for this:
using UnityEngine;
public class Attractor : MonoBehaviour
{
public Transform player;
private Rigidbody2D playerBody;
public float influenceRange;
public float intensity;
void Start()
{
playerBody = player.GetComponent<Rigidbody2D>();
}
void Update()
{
float distanceToPlayer = Vector2.Distance(player.position, transform.position);
if (distanceToPlayer <= influenceRange)
{
Vector2 pullForce = (player.position - transform.position).normalized * intensity;
playerBody.AddForce(pullForce, ForceMode2D.Force);
}
}
}
Intensity is set to 5 and influenceRange to 4.
All references are set up correctly.
In the Unity Editor the pull effect seems much stronger than on android though.
Pull Effect on android:
I can't show pull effect in the editor because I'm not home now, but there the ball flies through the sky and gets attracted much stronger.
Any help is appreciated!