Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 FranktheGame · Sep 21, 2020 at 06:02 PM · aiprojectileshoot

AI aim and shoot projectile at Player

I am trying to make an AI enemy shoot projectiles at the player taking into account their speed to shoot in front of them in the direction they are going. The projectile detects the player and shoots every second, but is just shooting straight. I am not sure if I am calculating the target wrong or not aiming the projectile correctly.

This is a simplified version to drop into an object to test with a projectile. Any thoughts? Is there anything else I should be calculating for this?

 public class AIShootProjectile : MonoBehaviour
 {
     public GameObject projectile;
     public SphereCollider sensorTrigger = null;
     public float shootForce;
     public float attackCoolDown = 1;
 
     private float timer;
 
     private Vector3 targetpos;
     private float distanceToTarget;
     private Vector3 targetDirection;
     private float targetSpeed;
 
 
     void Update()
     {
         timer += Time.deltaTime;
 
         if (timer >= attackCoolDown)  // if using ammo, add (&& ammo >= 1)
         {
             ShootProjectile();
         }
     }
 
 
     // AI has a large Sphere Collider attached to detect collisions of potential threats
     // When there is a collider, 
     void OnTriggerEnter(Collider other)
     {
         if (other.CompareTag("Player"))
         {
             // Get target info
             Vector3 velocity = other.attachedRigidbody.velocity;
             targetSpeed = velocity.magnitude;
             targetpos = other.transform.position;
             distanceToTarget = Vector3.Distance(sensorTrigger.transform.position, other.transform.position);
             targetDirection = other.transform.forward;
         }
     }
 
     void ShootProjectile()
     {
         float projectileSpeed = shootForce;
         float projectileTimeToTarget = distanceToTarget / projectileSpeed;
         float projectedTargetTravelDistance = targetSpeed * projectileTimeToTarget;
         Vector3 projectedTarget = targetDirection * projectedTargetTravelDistance;
         projectedTarget.y += 1; //aim at center of target if 2m high
 
         GameObject go = Instantiate(projectile, transform.position, Quaternion.identity);
         go.transform.LookAt(projectedTarget);
         Rigidbody rb = go.GetComponent<Rigidbody>();
         rb.velocity = go.transform.forward * shootForce;
         rb.useGravity = true;
 
         timer = 0f;
     }
 }
 
 
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ExtriPL · Sep 21, 2020 at 10:11 PM

Probably, you should add target position to projectedTarget

void ShootProjectile()
{
    ...
    Vector3 projectedTarget = targetPosition + targetDirection * projectedTargetTravelDistance;
    ...
}

You can also simplify code by using velocity vector instead of forward direction

 void OnTriggerEnter(Collider other)
 {
      if (other.CompareTag("Player"))
      {
          // Get target info
          targetVelocity = other.attachedRigidbody.velocity;
          targetpos = other.transform.position;
          distanceToTarget = Vector3.Distance(sensorTrigger.transform.position, other.transform.position);
      }
 }    
 void ShootProjectile()
 {
          float projectileTimeToTarget = distanceToTarget / shootForce;
          Vector3 projectedTarget = targetPosition + targetVelocity * projectileTimeToTarget;
          projectedTarget.y += 1; //aim at center of target if 2m high
           ...
 }


Comment
Add comment · Show 1 · 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
avatar image FranktheGame · Sep 21, 2020 at 10:53 PM 0
Share

Hey Thanks! Adding the target position to the projected target seemed to help and it's now shooting at the Player, but not taking into account the movement speed to shoot in front of the Player. At least one of the issues was that it's not getting the velocity of the Player so it's always Vector3 zero. I have tried a few different ways though doesnt seem to recognize the player's rigidbody.

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

221 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 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

Make AI enemies react to bullet hits (Solved) 0 Answers

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

Enemy (AI) Doesn't shoot weapon projectile, until after the player is gone. 0 Answers

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

how to make an enemy Ai blow up when you shoot it? 2 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