Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Hanktobe · May 27, 2014 at 02:43 AM · c#2dmovementtopdown

Enemy Movement Help

I am working on a topdown 2D game and was testing it when I saw that the enemies that spawned went to where the player was placed before it started moving. My code for the enemy movement is using UnityEngine; using System.Collections; public class EnemyMotion : MonoBehaviour { public float speed; public Transform player; void FixedUpdate() { float z = Mathf.Atan2 ((player.transform.position.y - transform.position.y), (player.transform.position.x - transform.position.x)) * Mathf.Rad2Deg - 90; transform.eulerAngles = new Vector3 (0, 0, z); rigidbody2D.AddForce (gameObject.transform.up * speed); } }

Please Help

Comment
Add comment · Show 2
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 meat5000 ♦ · May 28, 2014 at 10:41 PM 0
Share

Do you mean they spawned there, or moved there?

avatar image Kiwasi · May 29, 2014 at 02:19 AM 0
Share

Is player assigned correctly in the inspector?

If it is I would try add Debug.Log(z) at about line 9 and make sure that z is updating correctly as the player moves around.

3 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by Pyrian · May 28, 2014 at 09:27 PM

Change "public Transform player" to "public GameObject player". You want the enemies to follow the player's current position, not where he was before the game even starts.

Comment
Add comment · Show 3 · 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 Hanktobe · May 29, 2014 at 01:43 AM 0
Share

This didn't change anything. The enemy's that start on the screen work just as they are meant to but the ones from the spawner go to where the player was placed before the game started regardless of where the player is.

avatar image Kiwasi · May 29, 2014 at 02:01 AM 0
Share

Transform is also updated each frame and is the correct place to reference for getting position.

avatar image Pyrian · May 29, 2014 at 10:21 PM 0
Share

Try making sure it's set correctly when the new wave spawns. Since player is public, you should be able to set it by the spawner. Alternatively, you could do a tag search in the Start of the enemy, but that would be less efficient.

avatar image
0

Answer by jcoder · May 28, 2014 at 03:29 AM

I don't really see why it's a problem if the enemies follow the player when the player isn't moving. Maybe you meant the player wasn't active?

 public GameObject player;
 
 void FixedUpdate()
 {
     if(player.activeInHierarchy)
     {
         //follow player
 
     } 
 }
Comment
Add comment · Show 2 · 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 Hanktobe · May 28, 2014 at 09:00 PM 0
Share

They go to where the was placed before he started moving and only the first wave follows him.

avatar image meat5000 ♦ · May 28, 2014 at 10:41 PM 0
Share

Format code jcoder!

Highlight and click 1010101

Ill do this one for you

avatar image
0

Answer by meat5000 · May 29, 2014 at 02:07 AM

 public Transform player;

alone is not enough to retrieve updated information.

Your enemy will, in Start(), require to 'Find' the player Object and you will need to GetComponent(Transform) the result.

Any subsequent call to the players information will herald updated information.

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

24 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

How can I make 2D movement less jerky on a controller, with velocity and such? 0 Answers

2D camera zoom smoothing and limitations? 1 Answer

Enemy Knockback when hit player 2D Topdown 1 Answer

How to make sprites in SideScroller move in smooth curves 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