• 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
Question by tabbr17 · Feb 27, 2022 at 07:01 PM · scripting problemplayerenemyshootingenemy ai

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

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

The Enemy Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Enemy : MonoBehaviour
 {
     public float speed;
     public float stoppingDistance;
     public float retreatDistance;
     private float maxDistance;
     public int maxEnemyHealth;
 
     private float timeBTWShots;
     public float startTimeBTWShots;
 
     public Transform eFirePoint;
     private Transform player;
     public GameObject projectile; 
     public float projectileSpeed;
     private Vector2 target;
 
     private void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player").transform;
         target = new Vector2(player.position.x, player.position.y);
 
         maxDistance = stoppingDistance + retreatDistance * 2;
         timeBTWShots = startTimeBTWShots;
     }
 
     private void Update()
     {
         if(Vector2.Distance(transform.position, player.position) > stoppingDistance)
         {
             transform.position = Vector2.MoveTowards
             (transform.position, player.position, speed * Time.deltaTime);
         }
         if(Vector2.Distance(transform.position, player.position) < retreatDistance)
         {
             transform.position = Vector2.MoveTowards
             (transform.position, player.position, -speed * Time.deltaTime);
         }
         if(Vector2.Distance(transform.position, player.position) <= maxDistance
         || Vector2.Distance(transform.position, player.position) == maxDistance)
         {
             EnemyShoot();
         }
     }
 
     private void EnemyShoot()
     {
         if(timeBTWShots <= 0)
         {
             GameObject eBullet = Instantiate
             (projectile, eFirePoint.position, eFirePoint.rotation);
 
             projectile.transform.position = Vector2.MoveTowards
             (transform.position, target, projectileSpeed * Time.deltaTime);
 
             timeBTWShots = startTimeBTWShots;
         }
         else
         {
             timeBTWShots -= Time.deltaTime;
         }
 
         if(transform.position.x == target.x && transform.position.y == target.y)
         {
             Destroy(projectile);
         }
     }
 
     public void TakeDamage(int damage)
     {
         maxEnemyHealth -= damage;
         if(maxEnemyHealth <= 0)
         {
             Destroy(gameObject);
         }
     }
 }

Comment
VonKiver

People who like this

1 Show 0
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

2 Replies

  • Sort: 
avatar image

Answer by yonatanab1 · Feb 28, 2022 at 09:11 AM

first of all, the problem could be that the player is a child of an object that affects it's position. try to print "player.position" every frame (on update()) and check if it works.

second of all, your question is a bit unclear. is the bullet moving? is it moving to the wrong direction? what is the problem here exacly? (whats working and not againts what should work)

Comment
tabbr17

People who like this

1 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 tabbr17 · Feb 28, 2022 at 11:16 AM 0
Share

The player is not a child of another object, and the print(player.position); is working fine. The problem is that, when the bullet is instantiated, it does'nt move. I want the enemy to target the player and shoot, but it doesn't work.

avatar image

Answer by VonKiver · Feb 28, 2022 at 01:46 PM

I'd recommend you have one class for Enemy and another for Projectiles
Then for the target I'd have target = player.transform.position rather than new Vector2.
Then you don't need to have if(transform.position.x == target.x && transform.position.y == target.y) you can simply go by if (transform.position = target.position)
You also Instantiate a GameObject eBullet which I don't understand where's it coming from since you have a public GameObject projectile.
But I imagine the main issue is that you don't have a method for the attack? I'm not too sure tbh but maybe something like this would help?

 public void Attack()
     {
         Vector2 direction = player.position - eFirePoint.position;
         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
         Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
         eFirePoint.rotation = rotation;
 
         Instantiate(projectile, eFirePoint.position, eFirePoint.rotation);
     }

I hope it helps.

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

261 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

Related Questions

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

My enemy character is not facing the player correctly! 0 Answers

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

How do i make chaser in 2D enless runner? 1 Answer

Only one cloned enemy shoots 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