Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ThexMorgan · May 02, 2020 at 07:00 PM · angletargetviewtower-defense

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: alt text

 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  
     }


unknown-1.png (308.0 kB)
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image icehex · May 03, 2020 at 02:01 AM 0
Share

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!

avatar image ThexMorgan · May 03, 2020 at 03:47 AM 0
Share

@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

avatar image icehex ThexMorgan · May 03, 2020 at 04:14 AM 0
Share

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.

avatar image ThexMorgan icehex · May 04, 2020 at 09:53 PM 0
Share

@icehex Can you show me an example of how you would apply the solution with the boundaries in the code please?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges