How can make it so only 1 object of type “Ability” can be selected at once?
0
I am making an ability system in my game and am running into a few issues. I have set up a script that tells me which "ability" is closest to my cursor and changes the animation state if it is. With one of my abilities, the debug screen says "The closest ability is A" (which works) if selected, but if my cursor is closer to another, it says "The closest ability is A" followed by "The closest ability is B". This is screwing up my animations by triggering selected on both objects when only one should be selected. Where i my code is making this problem occur and how can I fix it so that only 1 ability can be selected at a time. Thanks in advance!
Closest to cursor script using System.Collections; using System.Collections.Generic; using UnityEngine;
public class AtMouseCursor : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
ClosestAbility();
Vector2 cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = cursorPos;
}
void ClosestAbility()
{
float distanceToClosestAbility = Mathf.Infinity;
Ability closestAbility = null;
Ability[] allAbilities = GameObject.FindObjectsOfType<Ability>();
foreach (Ability currentAbility in allAbilities)
{
float distanceToAbility = (currentAbility.transform.position - this.transform.position).sqrMagnitude;
if (distanceToAbility < distanceToClosestAbility)
{
distanceToClosestAbility = distanceToAbility;
closestAbility = currentAbility;
UnityEngine.Debug.Log("The Closest Ability is " + closestAbility.name);
}
}
foreach (Ability currentAbility in allAbilities)
{
if (currentAbility != closestAbility)
{
currentAbility.GetComponent<Ability>().AbilityNotSelected();
}
else if (currentAbility == closestAbility)
{
currentAbility.GetComponent<Ability>().AbilitySelected();
}
}
}
}
Ability Script (Goes on ability prefabs) using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Ability : MonoBehaviour
{
public Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
public void AbilitySelected()
{
UnityEngine.Debug.Log("Bruh im selscted");
anim.SetBool("isSelected", true);
}
public void AbilityNotSelected()
{
anim.SetBool("isSelected", false);
}
}
,0
I am making an ability system in my game and am running into a few issues. I have set up a script that tells me which "ability" is closest to my cursor and changes the animation state if it is. With one of my abilities, the debug screen says "The closest ability is A" (which works) if selected, but if my cursor is closer to another, it says "The closest ability is A" followed by "The closest ability is B". This is screwing up my animations by triggering selected on both objects when only one should be selected. Where i my code is making this problem occur and how can I fix it so that only 1 ability can be selected at a time. Thanks in advance!
Closest to cursor script using System.Collections; using System.Collections.Generic; using UnityEngine;
public class AtMouseCursor : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
ClosestAbility();
Vector2 cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = cursorPos;
}
void ClosestAbility()
{
float distanceToClosestAbility = Mathf.Infinity;
Ability closestAbility = null;
Ability[] allAbilities = GameObject.FindObjectsOfType<Ability>();
foreach (Ability currentAbility in allAbilities)
{
float distanceToAbility = (currentAbility.transform.position - this.transform.position).sqrMagnitude;
if (distanceToAbility < distanceToClosestAbility)
{
distanceToClosestAbility = distanceToAbility;
closestAbility = currentAbility;
UnityEngine.Debug.Log("The Closest Ability is " + closestAbility.name);
}
}
foreach (Ability currentAbility in allAbilities)
{
if (currentAbility != closestAbility)
{
currentAbility.GetComponent<Ability>().AbilityNotSelected();
}
else if (currentAbility == closestAbility)
{
currentAbility.GetComponent<Ability>().AbilitySelected();
}
}
}
}
Ability Script (goes on each ability prefab)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ability : MonoBehaviour
{
public Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
public void AbilitySelected()
{
UnityEngine.Debug.Log("Bruh im selscted");
anim.SetBool("isSelected", true);
}
public void AbilityNotSelected()
{
anim.SetBool("isSelected", false);
}
}
Your answer
Follow this Question
Related Questions
How can make it so only 1 object of type “Ability” can be selected at once? 0 Answers
Shooting bullets up (y axis) 0 Answers
Coyote Timer Gives Double Jump Frames. 1 Answer
How to have multiple enemies (currently having to kill them in order) 1 Answer
Camera Movement Question - How to follow player in Y axis only above certain threshold? 0 Answers