Hi, I am pretty new to Unity and programming in general, so I apologise if this code is looks weird/bad, I cobbled it together from my knowledge and youtube videos.
This script currently turns on a magnetism/follow if the player rolls passed a potential follower, which works well. I have been trying to make it so that once one follower has been ‘activated’ then no other follower could be activated until you have told your current follower to stop following you.
I’d really appreciate any help on this , or guidance as to what I should look into.
Thanks a lot
public float magnetStrength = 5f;
public float distanceStrength = 10f;
public int polarity = 1;
public bool magnetOn = false;
public bool stopFollow = false;
private Transform trans;
private Rigidbody thisRd;
private Transform playerTrans;
private bool magnetInZone;
void Awake()
{
trans = transform;
thisRd = trans.GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetButtonDown("e") && magnetInZone == true)
{
stopFollow = true;
}
if (stopFollow == true)
{
magnetOn = false;
}
}
void FixedUpdate()
{
if (magnetInZone && magnetOn)
{
Vector3 directionToMagnet = playerTrans.position - trans.position;
float distance = Vector3.Distance(playerTrans.position, trans.position);
float magnetDistanceStr = (distance / distanceStrength) * magnetStrength;
thisRd.AddForce(magnetDistanceStr * (directionToMagnet * polarity), ForceMode.Force);
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player" && stopFollow == false)
{
playerTrans = other.transform;
magnetInZone = true;
magnetOn = true;
}
}
}