So I made a turret that has a sphere as a trigger and shoots at the player when he enters it, which works fine, except it also shoots at the player when hes behind walls, and I don’t want that.
I thought about making a cone shaped trigger in front of the turret kind of like a view area but the turret would still shoot at the player behind a wall if the trigger was going through it, and I don’t want to have to individually resize the trigger for each scenario.
And my final conclusion was to make a child camera of the turret that would raycast and shoot only if it hits an object with a tag “Player”.
So I have a couple questions about that.
-
Is it a good way, or is it resource expensive?
-
Is there a better way?
-
How would I do it? Something like this like in the unity docs?
function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3(0,0,1), hit, 100.0)) {
var distanceToGround = hit.distance;
}
}
And then how would I make it react only to an object with tag and execute a function in its parent?
Raycast is really confusing to me even tho I can visualize it I don’t understand how the code works, none of the tuts on raycasting helped me, and I could never understand Unity docs. x’D
PS: Sorry for the code not being in code quotes its not working now for some reason, it does usually.