I have a monobehaviour script that represents a component shared by both the Enemy and Player “controller” classes in my space shooter. The script receives a variable that represents the target that their respective turrets are meant to rotate around.
The Enemy controller is an AI “brain” script, managed by a series of coroutines.
The Player controller is hooked up to player input component for keyboard/mouse control.
As such, the Players target is always the mouse cursor
The Enemy’s target is always the Player.
In either case both values will come to represent coordinates in space… --of the player as the target of the enemy, and of the screen-to-world position of the mouse as the target of the player. The problem is, one is a vector3, and the other is a transform. Right now, I have an overloaded method to differentiate between the two types in the setter.
And this takes place in the setter… essentially I have:
public Vector3 aimTarget
{
get => _aimTarget;
set => _aimTarget = SetAimTarget(value);
}
SetAimTarget is the overloaded method that is supposed to convert both the mouse position screen coordinates vector3 to world space vector3, and player transform to transform.position vector3 respectively and set it to the private _aimTarget variable for further processing and rotation calculations. In either case, the output will always be a vector3.
Thats what I’m trying to do… however, I’m getting an error for type mismatch when I send the transform to the setter. I know why I can’t do this, but I don’t know why I shouldn’t be able to, if that makes any sense lol. Why does the type of aimTarget matter here? Nothing is getting stored in aimTarget. Its getting stored in _aimTarget… --and after it has been returned as a Vector3 by SetAimTarget, which provides the overload for Vector3 inputs AND Transform inputs. Why does either aimTarget or _aimTarget have any clue what I’m putting into SetAimTarget and why should they care so long as the output matches their type? And most importantly, is there a clean way to do this? I’ve fiddled around with generics, but frankly, couldn’t figure it out in this particular case.