• 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 Dachckol · Aug 17, 2012 at 03:36 PM · playerenemystopping

Why is my enemy stopping before it reaches the player?

Hi, I have a problem with my enemy. The enemy is a model with a rigidbody attached to it. It is supposed to move towards the player over a map with lots of trees and some hills.

Everything works fine until the enemy gets relatively close to the player(about 40 map squares) and stops. To be exact it kind of jerks back and forth without moving towards its target. I have been trying a lot of things but the thing just won't work. Here is my code for the enemy moving:

 var player:Transform;
 var speed:float = 20;
 
 function Start()
 {
     player = GameObject.Find("First Person Controller").gameObject.transform;
 }
 
 function Update () 
 {
     transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(player.position-transform.position), speed);
     rigidbody.AddForce((player.position-transform.position)*speed*Time.deltaTime);
 }

As I said the enemy is a rigidbody and it has a mass of 10. Can anyone help?

Thank You,

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 JeanLuc · Aug 17, 2012 at 03:55 PM 0
Share

is your collider of your player is bigger then its $$anonymous$$esh ? it might be bouncing of the collider mesh/box/sphere...

avatar image Dachckol · Aug 17, 2012 at 04:42 PM 0
Share

The distance it stops at varies depending on the enemy and all the enemies use the same prefab. It couldn't be the colliders.

Good thinking though.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by ScroodgeM · Aug 17, 2012 at 03:41 PM

this line

rigidbody.AddForce((player.position-transform.position)*speed*Time.deltaTime);

gets lower and lower while time, cause distance gets lower. methinks speed should not depends on distance. and a little hint - apply physics in FixedUpdate

void FixedUpdate()
{
    rigidbody.AddForce((player.position-transform.position).normalized * speed);
}
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 Dachckol · Aug 17, 2012 at 04:53 PM 0
Share

Thanks for the reply! The thing is working now. A little note on the FixedUpdate:

The example you gave me is in C# would the function look the same in javascript with a 'function' ins$$anonymous$$d of 'void'?

avatar image Bunny83 · Aug 17, 2012 at 05:11 PM 0
Share

Both right ;)

+1

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Multiple Cars not working 1 Answer

Player lives script 1 Answer

I made my code so that if i hit an enemy its health will go down but when i hit the enemy the health doesnt go down. please help 1 Answer

Question on Player Damage 1 Answer

Enemy AI Help? 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