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 Cinnolate · Dec 05, 2017 at 10:35 AM · playerenemyshooting

How to make a moving enemy shoot at the player rather than the player's spawn?

Hi. I am making a top-down shooter and I was having an issue with getting a moving enemy to shoot at the player. The enemy in question shoots at the player's initial spawn but not at the player's current location. Below is the script that controls all enemies and enemies shooting. I could use some help.

 using UnityEngine;
 using System.Collections;

 public class Enemy : MonoBehaviour
 {
     public float speed;
     public string shipType;
     public float travelTime;
     public float restTime;
     public GameObject spawner;
     public GameObject bullet;
     public GameObject player;
     
     public bool canShoot;
     public bool lookAtPlayer;

     void Start()
     {
         float posX = transform.position.x + offsetRange * Random.value;
         Vector2 offset = new Vector2(posX, transform.position.y);
         transform.position = offset;
     }

     void Update()
     {
         ShipControl(shipType);
     }

     void ShipControl(string type)
     {
         switch(type)
         {
             case "basic": Move(); break;
             case "shooting": Move(); break;
             case "moveShoot": Move(); PointAt(); break;
         }
     }

     void Move()
     {
         if(travelTime > 0)
         {
             transform.Translate(Vector2.down * speed * Time.deltaTime);
             travelTime -= 1 * Time.deltaTime;
         }
         else if(restTime > 0)
         {
             if(canShoot)
                 Shoot();
             restTime -= 1 * Time.deltaTime;
         }
         else
         {
             if (canShoot)
                 Shoot();
             travelTime += 1;
             Move();
         }
     }

     void PointAt()
     {
         if(spawner != null && player != null)
             spawner.transform.up = player.transform.position - transform.position;
     }

     void Shoot()
     {
         if(bullet != null && spawner != null)
         {
             GameObject thisBullet = Instantiate(bullet, spawner.transform.position, spawner.transform.rotation) as GameObject;
             canShoot = false;
         }
             
     }
 }
Comment
Add comment · Show 3
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 Cinnolate · Dec 04, 2017 at 11:30 PM 0
Share

Sorry about the double post. This is my first time posting a question on here.

avatar image Kwergan · Dec 05, 2017 at 11:56 AM 0
Share

Are you sure you set GameObject Player to the player and not accidentally the spawn?

Also, you use the term "spawner" when calculating PointAt(). Could you clarify what this refers to?

avatar image Cinnolate · Jul 17, 2018 at 04:49 PM 0
Share

"spawner" refers to the EnemySpawner GameObject. The EnemySpawner has no components attached to it, other than the usual Transform Component.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vilhien · Jul 17, 2018 at 07:39 PM

I see where you are instantiating your bullets but what code re you using to move them. Also, are you updating the position when you call the bullets to fire? The sounds of it, that you call the position of the player in start only for the bullets. Which is why they would continue to only fire at that position, is my guess.

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

82 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

Related Questions

When the enemy character shoots, the bullet won't go to the position of my player! 2 Answers

Can someone help me to understand why this raycast is not working properly? 1 Answer

Enemy fire rate? help! 2 Answers

AI Script attached to Enemy and is Rotating around player 0 Answers

enemy AI doesnt work when enemy is spawned but does if it is already in the scene 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