Gameobject Gravity 2D Script
This is the script here You attach it to the player, then attach the desired game object that you want to be affected onto the component.
BUT the issue is that once the player is affected, no matter the distance, it is continually dragged towards the object...*
Is there a way to stop the player being affected when it has moved a certain distance away from the gameobject.
using UnityEngine; using System.Collections;
public class Gravity1: MonoBehaviour {
public float maxGravDist = 4.0f;
public float maxGravity = 35.0f;
public Rigidbody2D rb;
GameObject[] planets;
void Start () {
planets = GameObject.FindGameObjectsWithTag("Planet");
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate () {
foreach(GameObject planet in planets) {
float dist = Vector3.Distance(planet.transform.position, transform.position);
if (dist <= maxGravDist) {
Vector3 v = planet.transform.position - transform.position;
rb.AddForce(v.normalized * (1.0f - dist / maxGravDist) * maxGravity);
}
}
}
}
Your answer
Follow this Question
Related Questions
SImple but IMPOSSIBLE Enemies keep dying way ahead of time ! 0 Answers
How to Create Same Modes with different graphics? 0 Answers
How to control a random 3/4 Ratio? 0 Answers
[Question] Unity C# Set a Player Class and refence issue 0 Answers
Can someone please help me find out what wrong with my code. 0 Answers