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 CG-DJ · Aug 29, 2013 at 01:44 PM · collisionrigidbodyenemyvelocityaddforce

Rigidbody Enemy and Collisions

Ok,

I'm making a platformer game. My enemies have rigidbodys attached to them. I'm directly controlling their velocity by accessing the rigidbody.velocity component.

The problem is, when I have them chase me, they don't run into things and stop. I want them to hit other rigidbodys and try to push against them, but go no where. Do I need to use AddForce? Will AddForce with the VelocityChange force type give me enough control?

Thanks for the help! P.S UnityScript ;)

Link to the long script https://dl.dropboxusercontent.com/u/33872519/Rigidbody%20Enemy%20and%20Collisions/EnemyScript.js

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 Polymo · Aug 29, 2013 at 01:47 PM 0
Share

I think it's even noted in the docs, that if you manipulate velocity directly unexpected things may happen. Ins$$anonymous$$d you can use Rigidbody.$$anonymous$$ovePosition(Vector3 pos)

2 Replies

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

Answer by s_guy · Aug 30, 2013 at 04:27 AM

It's not recommended to move rigidbodies by changing their velocity directly (per docs and per your results). Instead use AddForce(), as you guessed, to get reasonable interactions with other rigidbodies, if you want acceleration (one of the main reasons for using a rigidbody). With this, your rigidbody can also get decent interactions with any animated colliders that have "animate physics" turned on in the Animation component checkbox. If you just want to move it without inertia, you can use MovePosition() and this will cause correct interactions with other rigid bodies.

Make sure to use AddForce() in FixedUpdate() to get correct acceleration. Conversely, never check Input.GetButtonDown() in FixedUpdate, you might miss some events--use the regular Update() for that, where the button down / up event is guaranteed for one frame.

You can still mess with rigidbody velocity directly with reasonable results if you're doing something instantly, i.e. one frame, and not constantly managing it, which seems to fight the physics engine.

ForceMode.VelocityChange will give you instant velocity change. It ignores mass and acceleration. To get a hang of the different force modes, a handy trick is to put the ForceMode type used in your public variables, so you can change it at runtime and see what it does.

If you really want direct and straightforward control, consider using a CharacterController (it's not just for player characters). That's the far less troublesome route unless you really need diverse and/or complex physics interactions with other rigidbodies. Often, it's easier to use the CharacterController component and apply simple, custom physics as needed, including pushing against rigidbodies.

If you're sure you want to use a rigidbody controller, you'll have to go to some trouble to avoid floaty, tedious to tweak, slow-to-start or fast-to-overshoot movement. The best way to do this is to make a motor component which manages linear acceleration to a target speed and angular acceleration to a target facing. Then, you can have a controller give higher level orders to move it around. There is a good example of this in the Angry Bots demo. The same low-level motor is used for player and NPC AI characters.

Good luck!

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 CG-DJ · Aug 30, 2013 at 01:28 PM 0
Share

Ok, I tried $$anonymous$$ovePosition and the problem is still there. The problem was, the mass of my rigidbody was too small. I had it at the default value of 1, but the enemy would just run through it then. When I set it to something like 5, the enemy would go half way through it, but when I set it to like 100, the enemy hit it and stopped in front of it. So, for anyone else with a similar problem, check the mass of your rigidbody!

But the $$anonymous$$ovePosition() function is great! Thanks!

avatar image
0

Answer by Joel_Ruiz_WV@yahoo.com · Aug 29, 2013 at 04:34 PM

I think using add force should solve your problem. If your enemy still doesn't collide with other rigid bodies, make sure your colliders aren't triggers and if your other rigid bodies are imported as a 3d model make sure import colliders is checked in your rigid body prefab. (:

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 CG-DJ · Aug 30, 2013 at 03:58 AM 0
Share

Ok, but it doesn't fix the problem. The enemy just walks through colliders. I have a box with a rigidbody attached to it. I know it works because my Character Controller runs into it and won't move. The enemy just walks through it though. What can I use so that it runs into it, but doesn't go through?

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

18 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

Related Questions

Rigidbody not calling OnCollisionEnter 1 Answer

How can I apply a force to an object in the opposite direction of the object it's colliding with? 2 Answers

Rigidbody Addforce cancels out Rigidbody velocity..maybe? 0 Answers

i want to make an animation throw code 0 Answers

collission not triggered when i touch object.. 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