Hello,
The following code is used to see if there is an enemy between the lines. If there is, the boolean will become “true”. But how can I assign the found enemy to a variable (public GameObject)?
enemyInRange = Physics2D.Linecast(rangeStart.position, RangeEnd.position, 1 << Layermask.NameToLayer("Enemy"));
So if there is an enemy and I would have a variable public GameObject target; how could I assign the enemy in the way to that variable?
How about:
if (enemyInRange != null && enemyInRange.collider != null) {
someBool = true;
someGameObject = enemyInRange.collider.gameObject;
}
Physics2D.Linecast returns a RaycastHit2D. I’m a bit surprised you CAN assign that to a Boolean, but you certainly shouldn’t if you want to know what is hit. Instead, assign it to a RaycastHit2D, then go through that to get the GameObject.
RaycastHit2D enemyInRange = Physics2D.Linecast(rangeStart.position, RangeEnd.position, 1 << Layermask.NameToLayer("Enemy"));
if (enemyInRange != null) {
GameObject EnemyHit = enemyInRange.rigidbody.gameObject;
}