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 Paul36 · Sep 03, 2012 at 06:35 PM · collisionmovementrigidbodydistance

Rigidbody slows down before touching its target

Hi, I am using a rigidbody and addForce to make an enemy move to the player, but when he is close, he slows down and moves too slowly to reach the player. I think it is because i calculate the distance between its position and the player's position, during a timestep, and if he comes too close, he only moves with a small distance :

alt text

But how should i do? I looked at the Angrybots project, but i have not seen something about that, maybe i misread the code.

Would you know how to avoid this issue? I would like the enemy to keep on rushing to the player until he touches him, not only until he gets close and then slows down.

This is the code :

         if (hit.collider.name==player.collider.name){
                     print("ENEMY SEES PLAYER");
                     var tempRot = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(directionToPlayer),Time.deltaTime);
                     tempRot.x=0;
                     tempRot.z=0;
                     transform.rotation=tempRot;
                     canSeePlayer=true;
                 }
             }
          
                 // Apply a force that attempts to reach our target velocity
                 var velocity = rigidbody.velocity;
                 var targetVelocity = player.transform.position - transform.position;
                 var velocityChange = (targetVelocity - velocity);
                 velocityChange.y = 0;
                 rigidbody.AddForce(velocityChange, ForceMode.Force);

Thanks

distance.png (23.2 kB)
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 Owen-Reynolds · Sep 03, 2012 at 10:52 PM

You are correct -- the problem is that `targetVelocity` depends on how far you are away. Not only does that make you creep when you get close, but enemies twice as far away move twice as fast. The trick is to turn targVel into a "standard length arrow" that points the way to move, not based on distance. Normalize does this. It makes any Vector3 be length 1, going the same direction.

In your code:

 // old line, to get way to move:
 var targetVelocity = player.transform.position - transform.position;
 // new lines to make speed not depend on distance:
 targetVelocity = targetVelocity.normalized; // Is now speed 1, going correct way
 targetVelocity = targetVelocity * MOVE_SPEED; // pick move_speed

Of course, you can do that all in one line.

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 Paul36 · Sep 04, 2012 at 10:23 AM 0
Share

@Owen Reynolds thanks Owen, okay it did not really know what was normalized ;-) it is more clear now, but in my case, there is a problem, the enemy does not move, or it is barely visible, even if i set move_speed to "5000.0", would you know why?

avatar image Paul36 · Sep 05, 2012 at 02:43 PM 0
Share

@Owen Reynolds hi, actually it works fine, thanks, i don't know why it did not work before, maybe an issue with the "cache" in Unity, but i'm not sure how to fix this, i needed to restart it again and 24h after, it works fine. Thanks for your help

avatar image Owen-Reynolds · Sep 05, 2012 at 05:46 PM 0
Share

Hmm..."quit and restart" is such a common solution for other programs, but I've only had one problem where I needed to restart Unity (a script that accidentally deleted the built-in Cube mesh.)

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make characters go through each other 1 Answer

Calling Rigidbody.MovePosition, no movement at all 4 Answers

Rigidbody Version of RotateAround 2 Answers

Projectile Ricochet Issue. Travels along the surface rather than reflecting off of it 2 Answers

Box collider (with Rigidbody attached) gets stuck into another Box 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