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 zenislev · Dec 01, 2009 at 05:53 PM · physicsgravity

Changing gravity affecting 'random movement' behaviour script - my NPCs go crazy, what should I do?

A forum mod kindly provided me the following code as an example of random walking behaviour:

var ground: Transform; var gravity: Vector3; var speed: float;

var motionDir: Vector3; var ctrl: CharacterController;

function Start() { ctrl = GetComponent(CharacterController); NewDirection(); }

function Update () { ctrl.Move((motionDir speed + gravity) Time.deltaTime); }

function OnControllerColliderHit(collision: ControllerColliderHit) { if (collision.transform != ground) { NewDirection(); } }

function NewDirection() { motionDir = (new Vector3(Random.value - 0.5, 0.0, Random.value - 0.5)).normalized; transform.rotation = Quaternion.LookRotation(motionDir); }

I'm attaching it temporarily to my NPC GameObjects. Testing it, it works well; the NPCs run around and change direction nicely. However, I found that I needed to increase the project gravity, because those GameObjects weren't responding properly at default settings. So, at -1500 gravity, my NPCs fall around satisfactorily, but - I assume because gravity is used in this code - they're now hyperactive; changing direction a few times per second and basically just bobbling around, instead of 'walking'.

I (naively) thought I could just divide the gravity value within this code to return the behaviour to normal, but that didn't work. How can I retain both the necessary gravity value in my project AND the 'normal' behaviour of this code? Or, is there a way to achieve 'proper' gravity in the scene without cranking it up to such a ridiculous value?

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
3
Best Answer

Answer by Jaap Kreijkamp · Dec 01, 2009 at 11:21 PM

-1500 gravity (guess you mean (0, -1500, 0)) sounds a bit high, that would mean a speed 1.5km per second (if using 1 unit == 1 meter).

If you haven't connected the object that's the ground to the NPC it starts to do this little dance, possibly you didn't see this behaviour earlier because gravity was 0. Currently the code allows only one object as ground and you have to set the ground property for all the NPC's, a better solution that allows more objects function as ground is:

function OnControllerColliderHit(collision: ControllerColliderHit) {
    if (collision.transform.tag != "ground") {
        NewDirection();
    }
}

Then create a tag 'ground' and set tag of all floor objects to this tag.

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 zenislev · Dec 02, 2009 at 12:11 AM 0
Share

Yeah, it sounds high to me too! I've got the scale wrong somewhere.

I didn't think that the ground was the problem, because I'd tried that with various Transforms and still got the madman's jig. For some reason though, your tag method has cleared it up! Nice work =D

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

No one has followed this question yet.

Related Questions

Planetary gravity for not so round object? 1 Answer

Pinball, Sphere and gravity, hitting sphere off the table 1 Answer

Constant force, with gravity, and correct collisions. 2 Answers

Planet gravity help need guidance no code 1 Answer

Faux Gravity Prolem? #2 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