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 SimonTheDiamond · Mar 07, 2012 at 09:54 AM · 2dmovementplayerenemyrange

Unity Enemy Range detection

I'm building a 2D game on Unity for my schoolproject and i'm currently working on enemies, who are suppose to within a certain range of the player, start following the player. Im using C#

I've just added the script so the enemy follows the players x position.

I was thinking that it should be something like this but other than that i have no idea...

if (transform.position.x = player.transform.position.x + 10)

{

if (transform.position.x > Player.transform.position.x)

{

Controller.Move(Vector3.left EnemySpeed Time.deltaTime);
}

else if (transform.position.x < Player.transform.position.x)

{

Controller.Move(Vector3.right EnemySpeed Time.deltaTime); }

}

Thanks for reading and i would appreciate any help you can give :)

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jakovd · Mar 07, 2012 at 10:29 AM

First of all, if you are doing it with Unity - it's a 3D game :) You may look at the scene with a ortographic view, and program it so that you ignore one axis, but it will still be represented as a 3D.

I can suggest you a better solution for your idea. You can subtract enemy position form player position to get a vector of direction from enemy to player. Then just move an enemy in that direction (multiplied with speed and deltaTime.) Why are you using Controller on enemies? If you need just that primitive "go-straight-toward-player" behaviour you can just move them by adding that direction to their transform.position value. Or, use rigidbodies on them and add force in that same direction.

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

Answer by SimonTheDiamond · Mar 09, 2012 at 08:35 PM

Thanks for the help! The problem is solved! :D

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

Answer by SimonTheDiamond · Mar 09, 2012 at 08:35 PM

The problem is solved! Thank you so much!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy collider used as range to detect player causes player to not go into jump animation, how should I deal with that? 0 Answers

2D Pathfinding Top Down 0 Answers

Move the enemy object opposite to player and keep its distance relative to the player? 1 Answer

How do I hurt enemies by jumping on them without getting killed? 1 Answer

Object: collider to floor, trigger to player 1 Answer


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