Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 DanielB89 · Oct 19, 2014 at 08:05 PM · aienemyprojectiledodge

Need help trying to make enemy detect player's projectiles

I'm trying to make an enemy ship to dodge the player projectile when in sight of it and continue to chase after the player. I couldn't find any proper tutorial on how to do this. When tried to get this to work, I ran into trouble with the enemies not tracking the player and following them.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyControllerBasic : MonoBehaviour {
 
 // The different IDS for the AI states
     enum AIMode { Normal, Ramming, SteerTowards, Avoid };
 
 
 void Start () {
  // find the player ship game object by name
         GameObject playerShip = GameObject.Find("PlayerShip");
         GameObject Bullet = GameObject.Find("PlayerProjectile");
         // Acess the player ship's script component by type
         PlayerShipCtrl = playerShip.GetComponent<ShipPlayerController>();
 
         
        // Always start in the normal state
         CurrentAIState = AIMode.Normal;
 
 }
 // Update is called once per frame
     void Update () {
         transform.position +=
                transform.up * Time.deltaTime * MoveSpeed;
         
         // Decides which AI state should be currently active
         DetermineAIState();
         EnemyPickup();
 
         // Depending on the current Ai mode
         switch (CurrentAIState)
         { 
             // Normal STATE
             case AIMode.Normal:
                 UpdateNormal();
                 break;
             // Rammin State
             case AIMode.Ramming:
                 UpdateRamming();
                 break;
             // STeer towards the player
             case AIMode.SteerTowards:
                 UpdateSteerTowards();
                 break;
 
         }
         
      
     }
 
 void UpdateNormal() { 
         // Move the object in the direction is facing
         transform.position +=
             transform.up * Time.deltaTime * MoveSpeed;
     
     }
     void UpdateRamming() { 
         // move the object in the direction it's facing
         transform.position +=
                 transform.up * Time.deltaTime * MoveSpeed;
     
     }
     void DetermineAIState() {
         bool canSee = CanSeeTarget("PlayerShip");
 
         // Subtract the enemy current position from the player
             Vector3 directionToPlayer =
                 PlayerShipCtrl.transform.position - transform.position;
 
 
             // Dot product of two vectors represent a cosine angle between them
             float product = Vector3.Dot(transform.up, directionToPlayer);
 
             // Convert the cosine value to a radian angle
             float angle = Mathf.Acos(product);
 
             // Convert the radian angle into a degree angle
             angle = angle * Mathf.Rad2Deg;
         if (canSee)
         {
             // If so, change to ramming mode
             CurrentAIState = AIMode.Ramming;
             
         }
         else if (product > 0 && angle < 90)
         { 
             // Change the state to steertoward
             CurrentAIState = AIMode.SteerTowards;
             // Change its color to green to enote
             renderer.material.color = Color.green;
         
         }
         else
         {
             // If not, return to normal state
             CurrentAIState = AIMode.Normal;
             renderer.material.color = Color.white;
             
         }
         
     }
 
     // Determines if the this AI can "See" the player
     bool CanSeeTarget(string _targetTag)
     {
         // Contains data about collision
         RaycastHit hitInfo;
 
         // Performs a ray cast
         bool hitAny = Physics.Raycast(transform.position, transform.up, out hitInfo);
 
         if (hitAny)
         {
             if (hitInfo.collider.gameObject.tag == _targetTag)
             {
                 return true;
             }
 
         }
 
         return false;
     }
 
 void UpdateSteerTowards() { 
      // Subtact the enemies current position from the player
         Vector3 directionToPlayer =
             PlayerShipCtrl.transform.position - transform.position;
     // rotate the enemy's movement direction 
         transform.up = Vector3.RotateTowards(transform.up,
                                             directionToPlayer,
                                             Time.deltaTime * MoveSpeed,
                                             0.0f);
         UpdateNormal();
     }
 
 void UpdateAvoid() {
         Vector3 directionToProjectile =
             Projectile.transform.position + transform.position;
         transform.up = Vector3.RotateTowards(transform.up,
                                                 directionToProjectile,
                                                 Time.deltaTime * MoveSpeed,
                                                 0.0f);
     
     }
     
 }
 
 
Comment
Add comment · Show 1
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 bubzy · Oct 19, 2014 at 09:29 PM 0
Share

you are not calling UpdateAvoid() anywhere.

edit : and your code doesn't work in anyway, so many references missing. is this something you have working in any way?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by LeftRight92 · Oct 20, 2014 at 04:21 AM

Fished out what bugs I could, I recommend you look into basic OOP, there are a lot of places where your trying to call a variable out of scope or are trying to create a variable without naming it. Note that in it's current state, the ship will only detect a bullet that existed before the ship did (as you are scanning for bullets in Awake()).

 using UnityEngine;
 using System.Collections;
  
 public class EnemyControllerBasic : MonoBehaviour {
      // The different IDS for the AI states
      enum AIMode { Normal, Ramming, SteerTowards, Avoid };
  
     private AIMode currentAIState = AiMode.Normal;
     private GameObject playerShip;
     private GameObject bullet;
     private PlayerShipCtrl playerController;
     
  
  void Start () {
     // find the player ship game object by name
     playerShip = GameObject.Find("PlayerShip");    
     bullet = GameObject.Find("PlayerProjectile");
     // Acess the player ship's script component by type
     playerController = playerShip.GetComponent<ShipPlayerController>();
  
  }
  // Update is called once per frame
      void Update () {
          transform.position +=
                 transform.up * Time.deltaTime * MoveSpeed;
          
          // Decides which AI state should be currently active
          DetermineAIState();
          EnemyPickup();
  
          // Depending on the current Ai mode
          switch (currentAIState)
          { 
              // Normal STATE
              case AIMode.Normal:
                  UpdateNormal();
                  break;
              // Rammin State
              case AIMode.Ramming:
                  UpdateRamming();
                  break;
              // STeer towards the player
              case AIMode.SteerTowards:
                  UpdateSteerTowards();
                  break;
  
          }
          
       
      }
  
  void UpdateNormal() { 
          // Move the object in the direction is facing
          transform.position +=
              transform.up * Time.deltaTime * MoveSpeed;
      
      }
      void UpdateRamming() { 
          // move the object in the direction it's facing
          transform.position +=
                  transform.up * Time.deltaTime * MoveSpeed;
      
      }
      void DetermineAIState() {
          bool canSee = CanSeeTarget("PlayerShip");
  
          // Subtract the enemy current position from the player
              Vector3 directionToPlayer =
                  playerShip.transform.position - transform.position;
  
  
              // Dot product of two vectors represent a cosine angle between them
              float product = Vector3.Dot(transform.up, directionToPlayer);
  
              // Convert the cosine value to a radian angle
              float angle = Mathf.Acos(product);
  
              // Convert the radian angle into a degree angle
              angle = angle * Mathf.Rad2Deg;
          if (canSee)
          {
              // If so, change to ramming mode
              currentAIState = AIMode.Ramming;
              
          }
          else if (product > 0 && angle < 90)
          { 
              // Change the state to steertoward
              currentAIState = AIMode.SteerTowards;
              // Change its color to green to enote
              renderer.material.color = Color.green;
          
          }
          else
          {
              // If not, return to normal state
              currentAIState = AIMode.Normal;
              renderer.material.color = Color.white;
              
          }
          
      }
  
      // Determines if the this AI can "See" the player
      bool CanSeeTarget(string _targetTag)
      {
          // Contains data about collision
          RaycastHit hitInfo;
  
          // Performs a ray cast
          bool hitAny = Physics.Raycast(transform.position, transform.up, out hitInfo);
  
          if (hitAny)
          {
              if (hitInfo.collider.gameObject.tag == _targetTag)
              {
                  return true;
              }
  
          }
  
          return false;
      }
  
  void UpdateSteerTowards() { 
       // Subtact the enemies current position from the player
          Vector3 directionToPlayer =
              playerController.transform.position - transform.position;
      // rotate the enemy's movement direction 
          transform.up = Vector3.RotateTowards(transform.up,
                                              directionToPlayer,
                                              Time.deltaTime * MoveSpeed,
                                              0.0f);
          UpdateNormal();
      }
  
  void UpdateAvoid() {
          Vector3 directionToProjectile =
              bullet.transform.position + transform.position;
          transform.up = Vector3.RotateTowards(transform.up,
                                                  directionToProjectile,
                                                  Time.deltaTime * MoveSpeed,
                                                  0.0f);
      
      }
      
  }
  
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make enemy to shoot and dodge projectile? 1 Answer

How to detect wether an object is headed towards the top or bottom of an object? 2 Answers

making a smart Enemy AI 1 Answer

Enemy AI Not Working? 1 Answer

How do I apply bullet projectile to enemy ai properly on unity3d 0 Answers


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