Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

How to create a gravitational pull effect on object?

$
0
0
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?

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>