Hello, im doing fake gravity towards center of planets using Sebastian Lague video tutorials. I managed to convert it to 2D, because im working in a 2D game. The thing is that everything works as expected but with just 1 planet on game. If i add more planets, it always take the last planet i added on the scene, maybe its because im just checking with tag, and all my planets have the same tag. How can i change it so the gravity force is towards the planet im touching the trigger.
This is the code:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody2D))]
public class GravityBody : MonoBehaviour
{
//Variables
private GravityAttractor planet;
private Rigidbody2D rb2D;
public bool attracting = false;
void Awake()
{
rb2D = GetComponent<Rigidbody2D>();
planet = GameObject.FindGameObjectWithTag("Planet").GetComponent<GravityAttractor>();
rb2D.gravityScale = 0;
}
void FixedUpdate()
{
if(attracting == true)
planet.Attract(transform);
}
void OnTriggerEnter2D()
{
}
void OnTriggerExit2D()
{
attracting = false;
}
}
planet = GameObject.FindGameObjectWithTag(“Planet”).GetComponent();
That part is the problem i think, becasue every planet in my game has that tag, so it just choose the last one i ad don scene.