- Home /
Aim and than shoot
I made some turrets for my game, each one can find me and it will start shooting when I get into it's shooting range.
The problem is that the turrets are shooting even if it not "aming" on me, it will shoot as long I'm in it's shooting range: for some the damping speed not allow them to always aim on me and it take some time untill thay are "realy aim" (it have to be like that), but thay will continue to shoot because the distance between me and the turrets allow it. The turrets will continue to shoot even if there is an obstacle like a wall or another turret between us from the same resone.
How do I make the turrets shoot only if thay are aming at the general direction (for missing chances) of me and when there is no obstacle between us?
Answer by Maulik2208 · Dec 24, 2012 at 08:39 AM
Follow the syntax showed below.....
Physics.SphereCast(/here fill as per your requirement/);
This is the simple way for your need.....Cheers......Enjoy......If found useful Don't forget to mark......
Ahh, I didn't thout this is a code... I still not sure how to fill it: I noticed "radius", which meen the turret's range, direction meen things like "forward" or "right", and this is it... How do I fill it as my requirement?
Answer by cagezero · Dec 22, 2012 at 09:16 PM
Use Physics.SphereCast. Set the radius to be your auto aim error, distance to be the range of your turret.
//Ray should be configured with the turrets position and whatever
//direction it is currently pointing (assuming Vector3.forward).
Ray = new Ray(turretRange.transform.position, Vector3.forward);
if(Physics.SphereCast(ray, autoAimError, hitInfo, turretRange)) {
//There are various ways to tell if this is the right collider.
//Here we will use the tag of the object we want to hit.
if(hitInfo.collider.gameObject.tag.CompareTo("target")) {
//The sphere cast hit the target. Shoot.
}
else {
//The sphere cast missed the target. Don't shoot.
}
}
Sorry, I don't have this option... I have SphereCollider, not SphereCast...
Use Physics.SphereCast from your turret. Once it detects that "you" are in range (using the algorithm you previously mentioned) you can begin to sphere cast from the turret to "you". This assumes that the sphere collider is attached the object that the turret is ai$$anonymous$$g at. If this is not the case please provide more details.
I don't understnd it yet. That what I did so far:
Physics.SphereCast(GameObject, 250, Vector3.forward,
I don't know how to continue and I don't understand what HitInfo meens.
Added some pseudo-code to the answer.
RaycastHit hitInfo contains information about what the sphere cast collided with. Use it to check if the turret has line of sight to the target (did the sphere cast hit the targets collider).
http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html
Your answer
Follow this Question
Related Questions
Enemy Aims at Player's Current Position 1 Answer
Bullet shots direction player is facing, cant shoot in diagnal or up 2 Answers
how do I shoot where my cross hair points? 0 Answers
Aim to crosshair? 3 Answers
3rd Person Gun Aim 1 Answer