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
1
Question by Zikku · Mar 19, 2012 at 08:37 PM · rigidbodyaienemygravityenemyai

Enemy Rigidbody Not Responding to Gravity

So I'm currently prototyping an enemy AI script (enemy model is just a red cube that is 2 units high, and 1 unit wide and long).

The enemy gets spawned through an empty game object. His first function is to patrol among several Waypoints (borrowed script from a tutorial). If he finds the player, he then calls a function to rotate and move toward the player. Once he's in a certain range, he then calls a function to attack.

Now I've had plenty of issues with that if statement, but the problem is his patrolling function. He was working before I commented stuff out to make him stop patrolling for fancy screen shots, but now he is no longer functional. Sort of.

When his prefab is spawned, he does nothing. It prints to tell me that he's started patrolling, but he's not even dropping to the floor (he spawns a unit above the floor and gravity is supposed to bring him down for him to patrol, which worked before like I said). Now he just floats at his spawn point, but his patrol function is being called.

After some troubleshooting some stuff, I finally disabled the spawn point, and brought the prefab directly in the scene. Oddly enough, the enemy will ONLY WORK CORRECTLY if I hit play while he's located within the range (a radius) that's defined as finding the player (so if he's close enough to the player). If I press start while he's farther than that range, he remains floating in space as before, having the same issue.

This is what is applied to the enemy prefab in the inspector:

  • Cube (Mesh Filter): Default Settings for a cube

  • Mesh Renderer: Default Settings

  • Character Controller

  •   Slope Limit: 45
    
  •   Step Offset:  0.3
    
  •   Skin Width:  0.08
    
  •   Min Move Distance: 0
    
  •   Center:  X:0, Y:0, Z:0
    
  •   Radius:  0.5
    
  •   Height:  1
    
  • Enemy AI (Script): My applied script

  • Rigidbody

  •   Mass:  100
    
  •   Drag:  0
    
  •   Angular Drag:  0
    
  •   Use Gravity:  True
    
  •   Is Kinematic:  False
    
  •   Everything else is default, except all rotations are checked for frozen.
    
    

And here is the entire code. I'd provide snippets instead, but I just don't know what it is that is causing the problem. Also, I'm not too familiar with the Couroutine and While statements, so if something can be done better, please let me know. Thanks

I saved it as a text file for anyone that would like to view it. enemyAI.txt

enemyAi.txt (5.6 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
1
Best Answer

Answer by aldonaletto · Mar 19, 2012 at 09:14 PM

This script uses SimpleMove, which takes care of gravity for you - but there's no gravity when SimpleMove isn't being called.
You can try to add a SimpleMove(Vector3.zero) to the Update function - it will not move the character, but the gravity will be applied.
Insert this instruction in the first line of Update():

   GetComponent(CharacterController).SimpleMove(Vector3.zero);

NOTE: Rigidbodies and CharacterControllers aren't a good combination. Delete the rigidbody or, if you really need it, set Is Kinematic to avoid weird effects.

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 Zikku · Mar 19, 2012 at 10:45 PM 0
Share

Ok, I'll try that next to see what happens. Thanks!

avatar image FloshPlosh · Jan 17, 2013 at 10:47 AM 0
Share

really, thank you so muck Aldo! I've been looking for something to help me aswell, with the same problem (but i don't use a rigidbody). This stopped my characters from falling through the ground and from flying while trying to chase me! thanks a billion :p

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 Follow AI problem 0 Answers

How do you move did with [iTWEEN] the enemy has a component of [Character Controller]? 0 Answers

Enemy Ai Problem with the collider and gravity 1 Answer

[I REALLY NEED HELP FAST]Help with enemy Shooting 1 Answer

how to add gravity to this script 2 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