- Home /
How to create a view angle where the turret can see the enemy and start shooting
This is the script of my turret, how can I create a certain angle of view where the turret starts following the enemy? like this:
public class Turret : MonoBehaviour
{
[Header("ATTRIBUTI")]
public float speed = 10f;
public float range = 10f;
public float fireRate = 1f;
private float fireCountdown = 0f;
public float damage;
public Bullet mybullet;
[Header("Unity")]
public GameObject bulletPrefab;
public Transform target;
public Transform firePoint;
public string enemyTag = "Enemy";
Vector3 lastKnowPosition = Vector3.zero;
Quaternion lookAtRotation;
void Start()
{
InvokeRepeating("UpdateTarget", 0f, 0.5f);
mybullet.Damage = damage;
}
void Update()
{
if (target == null)
return;
FollowTarget();
if(fireCountdown <= 0f)
{
Shoot();
fireCountdown = 1f / fireRate;
}
fireCountdown -= Time.deltaTime;
}
#region Shoot
void Shoot()
{
GameObject bulletGO = (GameObject) Instantiate(bulletPrefab, firePoint.position, Quaternion.identity);
Bullet bullet = bulletGO.GetComponent<Bullet>();
if(bullet != null)
{
bullet.Seek(target);
}
}
#endregion
#region Tracking Update
void UpdateTarget()
{
GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
float shortestDistance = Mathf.Infinity;
GameObject nearestEnemy = null;
foreach (GameObject enemy in enemies)
{
float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
if (distanceToEnemy < shortestDistance)
{
shortestDistance = distanceToEnemy;
nearestEnemy = enemy;
}
}
if (nearestEnemy != null && shortestDistance <= range)
{
target = nearestEnemy.transform;
}else
{
target = null;
}
}
#endregion
#region Follow Target
void FollowTarget()
{
if (target)
{
if (lastKnowPosition != target.transform.position)
{
lastKnowPosition = target.transform.position;
lookAtRotation = Quaternion.LookRotation(lastKnowPosition - transform.position);
}
if (transform.rotation != lookAtRotation)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, lookAtRotation, speed * Time.deltaTime);
}
}
}
#endregion
#region Gizmos
void OnDrawGizmos()
{
Gizmos.color = Color.blue;
Gizmos.DrawSphere(transform.position, range);
}
#endregion
}
Can you explain the picture more? What are A, B, and C and how do they show what you're wanting to get done? Thanks!
@icehex A, B, and C represented the turret while the colored squares the area of view of the turrets. For example, I'd like to know how to make turret B detect that there are enemies and that they can only be fired in the range of that view and how i can modify the variables to create for every turret the area of view i want
Probably the most straightforward way is to define your boundaries for the areas you wish the individual turret to shoot in, and then after confir$$anonymous$$g that the enemy is within the turret's range, which the above code does, do another if-statement to check if the target's position is inside of those boundaries. There are many ways you could check the boundaries, but if you have specifically a grid like in your picture, you could define a List of columns where each column has maximum and $$anonymous$$imum x and y boundaries (four variables). Then check against each item in the List to see if the player's position is within the boundaries.
Another technique, you could create some colliders with onTrigger which would pass the enemy's transform to the enemies GameObject, and you do this ins$$anonymous$$d of populating enemies with every object that has enemyTag.
@icehex Can you show me an example of how you would apply the solution with the boundaries in the code please?
Answer by fblast1 · May 06, 2020 at 05:01 AM
you can use this code and tweak it. It is fairly simple , refer to dot product for vectors . Then you can rotate your turret as needed to shoot
public Transform turret, player;
public Vector3 targetPos, fw;
// Update is called once per frame
void Update()
{
//Assuming Z forward
fw = turret.TransformDirection(Vector3.forward);
targetPos = player.position - turret.position;
if (Vector3.Dot(fw.normalized, targetPos.normalized) > 0.5) // > 0 Ahead (180 FOV), < 0 behind (180 FOV) tweak as needed for fov
{
print("Player Detected");
}
}
Regards
Your answer
Follow this Question
Related Questions
Help With Laser Effect - Perlin 2 Answers
Rotation Angle across 0, opposing angle? 1 Answer
camera target keep angle and y position 1 Answer
Attack multiple targets in the same tag? 1 Answer
Angle to Direction 1 Answer