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 junlee · Mar 24, 2012 at 07:21 AM · physicsaicharactercontrollervelocity

CharacterController causing characters to fly around screen

I am using a CharacterController on AI controlled mobs in my game. The AI follows a simple waypoint system I made by targeting a waypoint and turning towards it and running forward using CharacterController.SimpleMove

Here is a snippet of my code that moves the AI:

 var temp : Vector3 = Vector3(nextTarget.position.x, myTransform.position.y, nextTarget.position.z);
             if((temp - myTransform.position) != Vector3.zero)
                 myTransform.rotation = Quaternion.Slerp(myTransform.rotation, 
                 Quaternion.LookRotation (temp - myTransform.position), rotationSpeed * Time.deltaTime);        
             //move towards target
             controller.SimpleMove(myTransform.forward * curSpeed * Time.deltaTime);

This works fine, unless there is a lagspike, which causes all of the AI controlled characters in the game to wildly fly off screen. This can be reproduced 100% of the time by playtesting inside the unity editor, then holding left click on the top window bar of the unity editor (as if you were dragging the editor window around your screen, which causes the game to freeze) then letting go.

My game is being run through the web player, and it also happens there whenever any sort of FPS drop occurs from outside lag sources (such as having the game open in the browser, then clicking the play button inside unity to playtest there at the same time).

I tried to workaround this by resetting the position of the characters to their last visited waypoint if they don't reach their destination within 10 seconds, however their velocity from being thrown around stays with them and they continue to fly around after resetting position.

Is there something I'm doing wrong in my movement script? Is there a way to fix this?

Thanks

Comment
Add comment · Show 4
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 hijinxbassist · Mar 24, 2012 at 08:31 AM 0
Share

As a work around... ins$$anonymous$$d of waiting 10 seconds, state if the velocity is greater than an exagerated known velocity. if your enemy can only move at say 10, say if(enemy.velocity>=20)//move to last waypoint.

I will test this without Char Cntl and post results if i find prob in code.

avatar image junlee · Mar 24, 2012 at 07:46 PM 0
Share

I noticed that if I take Time.deltaTime out of my move equation, this doesn't happen. I thought using Time.deltaTime was supposed to make everything consistent regardless of fps...any ideas as to why this is happening?

avatar image asafsitner · Mar 25, 2012 at 09:56 AM 0
Share

You can store an array of the last, say, 5 Time.deltaTime values and use the lowest value as your multiplier. This happens because when you hold the title bar the application pauses and this causes the value of Time.deltaTime to get wildly big (I've tested this and it grows to it's max value of 0.3333333 ins$$anonymous$$d of more common values like ~0.015)

avatar image AlucardJay · Mar 25, 2012 at 06:50 PM 0
Share

http://unity3d.com/support/documentation/Components/class-Time$$anonymous$$anager.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zerot · Mar 25, 2012 at 02:38 PM

Change Time.deltaTime to Time.smoothDeltaTime. smoothDeltaTime is smoothed over multiple frames.

Time.deltaTime does make your game framerate independent, but not time independent. Time.deltaTime is nothing else then the time it took for the last frame to render. So at 60 fps it would be 1/60 = 0.016. At 10 fps the value would be 0.1. This value you then multiply with your normal movement, which makes it basically movement per second.

What happens, is that the deltaTime becomes a huge number because of the spike, which causes huge movement. For example, a 3 second spike causes a deltatTime of 3 seconds. Which means you move your character 3 seconds worth of time. Which is not what you want most of the time.

Another solution would be to have a max for your deltaTime with Mathf.Min(deltaTime, maxDeltaTime).

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 asafsitner · Mar 25, 2012 at 06:33 PM 0
Share

From what I've seen Time.deltaTime doesn't ever go past 0.333333

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

7 People are following this question.

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

Related Questions

How to transform velocity to world space before calling controller.move? 1 Answer

Removing velocity relative to the contact normal 1 Answer

Throwing knife doesn't throw correctly 0 Answers

How to make my Character run through walls with the use of the Character Controller component 1 Answer

Angular Friction 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