• 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 /
  • Help Room /
avatar image
0
Question by AnthonySpering · 2 days ago · rotationenemyprojectilerotatearound

How do I get the enemy's to rotate to players and shoot at them?

So I am making a game where the enemy is frozen in place and shoots the player, if they hit the player the game resets. The player is able to destroy them by killing them with their own bullets. I got the player and the enemy to shoot but the problem I am having is that the enemy does not shoot in the direction of the player. I can either get the enemy to shoot just in a straight line or I can get it to rotate towards the player but it does not rotate fast enough. Another problem I have is that if the player stands to the enemy's left, the enemy does not shoot in their direction even when the enemy is supposed to be rotating towards the player. In other words, the enemy does not shoot to the left. Can someone help me fix this? (It is a 3d game BTW) Here is the code for when the enemy only shoots in a straight line. To make the enemy stay still I froze its positions in place.

{

 public Rigidbody bulletPrefab;
 public float shootSpeed = 300;
 public Rigidbody self;
 public bool playerInRange = false;
 private float lastAttackTime = 0f;
 private float fireRate = 0.5f; //how many bullets are fired/second
 public Transform player = null;
 public Transform Target;



 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "player")
     {
         playerInRange = true;
         player = other.transform;
     }
 }

 void OnTriggerExit(Collider other)
 {
     if (other.tag == "player")
     {
         playerInRange = false;
         player = null;
     }
 }

 void Update()
 {
     transform.LookAt(Vector3.zero);
     if (playerInRange)
     {
         //Rotate the enemy towards the player
         transform.LookAt(Target);

         if (Time.time - lastAttackTime >= 1f / fireRate)
         {
             shootBullet();
             lastAttackTime = Time.time;
         }
     }
 }

 void shootBullet()
 {

     var projectile = Instantiate(bulletPrefab, transform.position, transform.rotation);
     //Shoot the Bullet in the forward direction of the player
     projectile.velocity = transform.forward * shootSpeed;

 }

}

Here is the code that makes the enemy rotate towards the player but the enemy doesn't rotate fast enough and does not shoot to the left. It is practically the same code as before but I just changed a few things. {

 public Rigidbody bulletPrefab;
 public float shootSpeed = 300;
 public GameObject target;
 public Rigidbody self;
 public bool playerInRange = false;
 private float lastAttackTime = 0f;
 private float fireRate = 0.5f; //how many bullets are fired/second
 public Transform player = null;



 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "player")
     {
         playerInRange = true;
         player = other.transform;
     }
 }

 void OnTriggerExit(Collider other)
 {
     if (other.tag == "player")
     {
         playerInRange = false;
         player = null;
     }
 }

 void Update()
 {
     transform.LookAt(Vector3.zero);
     if (playerInRange)
     {
         //Rotate the enemy towards the player
         transform.rotation = Quaternion.LookRotation(player.position - transform.position, transform.up);


         if (Time.time - lastAttackTime >= 1f / fireRate)
         {
             shootBullet();
             lastAttackTime = Time.time;
         }
     }
 }

 void shootBullet()
 {

     var projectile = Instantiate(bulletPrefab, transform.position, transform.rotation);
     //Shoot the Bullet in the forward direction of the player
     projectile.velocity = transform.forward * shootSpeed;
     projectile.transform.rotation = transform.rotation;
 }

} And if it helps here is the code I have for the enemy bullet.

{

 void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.CompareTag("Player"))
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
     }
 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

236 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 avatar image avatar image avatar image 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

How to limit rotation from RotateAround? 1 Answer

Rotating around another object without parent/child relation?? 0 Answers

Linking projectiles to enemy 0 Answers

How can I find the angle an object needs to turn towards a target from a rotated perspective? 0 Answers

Rotate around a point with a specific angle 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