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
1
Question by GreenPlanetGames · Dec 12, 2021 at 03:27 PM · 2dunity 2denemyshoot

Can someone Tell me how I can make the enemy shoot the player when the see them in 2D?

Hi, I'm currently making a small shoot em' up game and I've hit a roadblock when trying to find a way for the enemy's to shoot the player when they see them. I've tried everything (I think) and I've watched pretty much ever video on YouTube to try and find an answer. Could someone please tell me and/or give me a script to make the enemy shoot.

Thanks.

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 EvilBob99 · Dec 12, 2021 at 07:10 PM 0
Share

@animaldude77 did my code help at all

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by EvilBob99 · Dec 12, 2021 at 03:36 PM

so if you can find a way to make the enemy to rotate towards the player so its always looking at the player if you have an existing shooting function you can copy it over to the enemy script and have it draw a circle around the enemy that when the player gets within that circle it starts the aiming function and then when the player gets within another circle that's smaller it will start calling the shoot function.

I would give you all the code but its for a 3d enemy but the basic logic still applies if you can get anything out of the code here

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;

public class enemy : MonoBehaviour { public NavMeshAgent AI;

 public Transform player;

 public LayerMask whatisground, whatisplayer, whatisbullet;

 public float health;

 //patrolling
 public Vector3 walkpoint;
 bool walkpointset;
 public float walkpointrange;

 //attacking
 public float timebettweenattacks;
 bool alreadyattacked;
 public GameObject projectile;

 //states
 public float sightrange, attackrange;
 public bool playerinsightrange, playerinattackrange;

 private void Awake()
 {
     player = GameObject.Find("player").transform;
     AI = GetComponent<NavMeshAgent>();
 }

 private void Update()
 {
     //check for sifgt and attack range
     playerinsightrange = Physics.CheckSphere(transform.position, sightrange, whatisplayer);
     playerinattackrange = Physics.CheckSphere(transform.position, attackrange, whatisplayer);

     if (!playerinsightrange && !playerinattackrange)
     {
         patroling();
     }

     if (playerinsightrange && !playerinattackrange)
     {
         chaseplayer();
     }

     if (playerinsightrange && playerinattackrange)
     {
         attackplayer();
     }
 }

 private void patroling()
 {
     if (!walkpointset)
     {
         searchwalkpoint();
     }

     if (walkpointset)
     {
         AI.SetDestination(walkpoint);
     }

     Vector3 distancetowalkpoint = transform.position - walkpoint;

     //walkpoint reached
     if (distancetowalkpoint.magnitude < 1f)
     {
         walkpointset = false;
     }
 }

 private void searchwalkpoint()
 {
     //calculate random point in range
     float randomz = Random.Range(-walkpointrange, walkpointrange);
     float randomx = Random.Range(-walkpointrange, walkpointrange);
     walkpoint = new Vector3(transform.position.x + randomx, transform.position.y, transform.position.z + randomz);
     if (Physics.Raycast(walkpoint, -transform.up, 2f, whatisground))
     {
         walkpointset = true;
     }
 }

 private void chaseplayer()
 {
     AI.SetDestination(player.position);
 }

 private void attackplayer()
 {
     //attack code here
     Rigidbody rb = Instantiate(projectile, transform.position, Quaternion.identity).GetComponent<Rigidbody>();

     rb.AddForce(transform.forward * 32f, ForceMode.Impulse);
     rb.AddForce(transform.up * 8f, ForceMode.Impulse);


     //make sure your enemy doesnt move
     AI.SetDestination(transform.position);

     transform.LookAt(player);

     if (!alreadyattacked)
     {
         alreadyattacked = true;
         Invoke(nameof(resetattack), timebettweenattacks);
     }
 }

 private void resetattack()
 {
     alreadyattacked = false;
 }

 public void takedamage(int damage)
 {
     health -= damage;

     if (health <= 0) Invoke(nameof(destroyenemy), 0.5f);
 }

 private void destroyenemy()
 {
     Destroy(gameObject);
 }

 private void OnDrawGizmosSelected()
 {
     Gizmos.color = Color.red;
     Gizmos.DrawWireSphere(transform.position, attackrange);
     Gizmos.color = Color.yellow;
     Gizmos.DrawWireSphere(transform.position, sightrange);
 }

}

hope this helps

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

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

Making an enemy shoot at the Players direction (not just up, down, left and right) 1 Answer

How do I enable ShadowCaster2d? 1 Answer

Damaging Enemies 1 Answer

Enemy is not chasing player. it is escaping. 1 Answer

Unity doesn't compress 2d sprite skeleton animation in build/deploy? 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