• 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 tabbr17 · Feb 27 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
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by yonatanab1 · Feb 28 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
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 tabbr17 · Feb 28 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
0

Answer by VonKiver · Feb 28 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
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

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 can I make the EnemyBullet go further than my target? 0 Answers

Only one cloned enemy shoots 2 Answers

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

Enemy ai Ignore walls! help! 1 Answer

Run coroutine only when player stays inside collider? 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