I am trying to make a black hole kind of obstacle in a 2D platformer I am working on. I will have to fine tune the forces and such.
But there is a problem. The effect only seems to apply in the Y axis.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CircleCollider2D))]
public class BlackHoleScript : MonoBehaviour {
private float pullForce = 0.5f;
private float radius = 3f;
void Awake()
{
CircleCollider2D circleCollider2D= GetComponent();
radius = circleCollider2D.radius;
}
void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
float distance = Vector2.Distance(this.transform.position, other.gameObject.transform.position);
float force = pullForce * (radius / distance);
float x = this.transform.position.x - other.gameObject.transform.position.x;
float y = this.transform.position.y - other.gameObject.transform.position.y;
Debug.Log(x);
Debug.Log(y);
other.gameObject.rigidbody2D.AddForce(new Vector2(x*force, y*force), ForceMode2D.Impulse);
}
}
}
The x position of the object seems to be completely unaffected. What is it I am doing wrong?
↧