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 Krebzor · May 07, 2020 at 04:13 PM · enemymovement scriptenemy aienemy spawnenemys

I need help with Enemy following my Player

Hello everyone, this is kind of a specific question, so will try to clarify as much as I can. I have a 2D top shooter game, where I instantiate Enemies from different points and they follow my Player until they touch him and kill him. I already have set colliders, spawning points and the wave spawner and the enemies do follow properly the Player, but only when they already start in the game itself.

When I instantiate them they all start without the variable referenced that they have to specifically follow the player(its transform). So I made a prefab of the player and made them reference that. However now the enemies that I instantiate they go towards the original Transform point where my Player started and thats it, they never keep on looking for the new position the Player has, only the original position when game started.

The script for the enemy and its movement is the following:

public class Enemy : MonoBehaviour {

 public Transform player;

 public float moveSpeed = 4f;

 private Vector2 movement;
 private Rigidbody2D rbEnemy;

 // Start is called before the first frame update
 void Start()
 {
     
     rbEnemy = this.GetComponent<Rigidbody2D>();
 }

 // Update is called once per frame
 private void Update()
 {
     Vector3 direction = player.position - transform.position;
     float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - 90f;
     rbEnemy.rotation = angle;
     direction.Normalize();
     movement = direction;
 }

 private void FixedUpdate()
 {
     moveCharacter(movement);
 }

 void moveCharacter(Vector2 direction)
 {
     rbEnemy.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
 }

 private void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.tag == "Player")
     {
         Destroy(GameObject.FindGameObjectWithTag("Player"));
     }
     
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by iveL_newO · May 07, 2020 at 04:54 PM

It looks like you're setting the player transform variable in the editor. This would mean that instantiated versions don't have that set (unless you mess with the prefab I believe). A way to get around this would be to searching for the player when an enemy is instantiated like this:

player = Gameobject.Find("Your player name").transform;

This isn't optimal, however, because you're going to be searching a whole lot which isn't great for performance. Instead, you could store the player's transform in the script you're using to instantiate enemies. Then, whenever you instantiate an enemy you could modify the public player variable on your new enemy's script. So you would add a variable called "player" to the script instantiating enemies:

Public Transform player;

Then instantiate your enemies like this:

Instantiate(EnemyPrefab) as GameObject.getComponent<Enemy> ().player = player;

(If you're already storing the instantiated object in the script already then just use everything after .getComponent)

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 Krebzor · May 07, 2020 at 05:29 PM 0
Share

thanx! I actually went with the first fix, I kinda had something like that in $$anonymous$$d, but it is just for a small game so performance was fine.

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

131 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

Related Questions

Enemy randomly disappears on startup 0 Answers

Enemy ia fliping when hitting object 2 Answers

Enemy not generating... 0 Answers

I changed my monitor resolution and now my enemies are chasing me too fast ?? 2 Answers

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