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 Alex808 · Mar 07, 2013 at 10:28 PM · playerenemymovecollidetowards

Getting enemy to move towards player untill they collide problem...

So here is the problem I want the enemy to move towards the player until there is a collision here is the important part of my enemy script:

   function Update () {
      //rotate to look at the player
      myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
      Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
  
     //move towards the player
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 }

Now everything is setup to detect the collision and react properly but with the code above the enemy and player NEVER collide the enemy gets close to the player and starts to spin around the player without ever colliding...

Is the code above causing the problem?

Any solutions?

Comment
Add comment · Show 1
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 flaviusxvii · Mar 07, 2013 at 10:35 PM 0
Share

If the player moves too far on each step the colliders might jump past eachother without ever having the chance to detect collision.

This script can help. http://wiki.unity3d.com/index.php?title=DontGoThroughThings

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Alex808 · Mar 08, 2013 at 02:14 AM

The player can completely stop moving the enemy will move forward towards the player and within a certain distance the enemy will start to orbit around the player instead of colliding with the player ..

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 BenKurdziel · Mar 08, 2013 at 02:13 AM

You don't have a catch for collision in that code. So it will do that no matter what, even if you collide. Put a Box Collider around the enemy and set it to IsTrigger. Check OnTriggerEnter (other : Collider) to see if that other object is the player. If it is, set your speed to 0 (assuming you want them to stop moving).

Another option is to test their distance from each other and if their distance is less than a number you choose, you stop the NPC.

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 Alex808 · Mar 08, 2013 at 03:45 AM 0
Share

Everything is already in place to detect the collision and that part works..But with the code above the enemy never comes close enough collides with the player to collide with it.. It goes forward towards the player and at a certain distance before colliding with the player the enemy starts orbiting around the player. I did the test if I remove the code above that makes the enemy follow and move towards the player and i run the player into the enemy the collision IS detected.

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

13 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

Related Questions

Enemy Nudging Towards Player 1 Answer

Can anyone tell me whats wrong with my MoveTowardsEnemy part of my script? 1 Answer

Player Health 3 Answers

How can I make player move with cursor?,How can I make player move with mouse? 1 Answer

Teleporting Script Doesn't Work With Anything But The 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