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 Koritsuyuutsu · Apr 28, 2013 at 10:36 PM · javascriptplayermovingifconsole

"If player is moving, return true" - not entirely working

Hello, I've been trying to get the console to log "true" if the player is moving and "false" when it's not. I'm running into some potential trouble.

This is my script:

 var lastPos: Vector3; 
 
 function Start() {
     lastPos = transform.position;
 }
 
 function CharMoved() : boolean {
     var displacement = transform.position - lastPos; 
     lastPos = transform.position;
     return displacement.magnitude > 0.001; 
 }
 
 function Update() {
     print(CharMoved());
 }

This was taken from this question here.

The problem I have is that when you're not moving, it seems to be working like normal - Constantly logging "false". But when you move, it appears to be working as it begins to log "true", but then it begins logging "false" for every 6 true's or so.

Any suggestions why it's doing this, how to avoid/fix it, or another way of doing this?

Here's an image of the console:

alt text

Thank you!

capture.png (45.0 kB)
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 DESTRUKTORR · Apr 28, 2013 at 10:39 PM 0
Share

How are you handling the player's movement...? Are you handling user-input with FixedUpdate? If so, you should probably use regular Update, in s$$anonymous$$d, since FixedUpdate isn't necessarily called every frame, and can render user input quite buggy.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by whydoidoit · Apr 28, 2013 at 10:38 PM

I guess it depends on how fast it's moving :D If you've got high FPS then it could fail that test. You need to make it frame rate independent.

You should probably do this:

 function CharMoved() : boolean {
     if(Time.timeScale == 0) return false;
     var displacement = transform.position - lastPos; 
     lastPos = transform.position;
     return displacement.magnitude/Time.deltaTime > 0.001; 
 }
     
Comment
Add comment · 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
0

Answer by s_guy · Apr 29, 2013 at 01:01 AM

I don't think physics calculations are guaranteed to be evaluated between Update() calls. So, maybe Update() can fire multiple times without anything having an updated position, even though the object hasn't really "stopped".

Some options to try:

  • If you're using a character controller, you can test myController.velocity == 0;

  • If you're using a rigid body on the controller, you can test rigidbody.velocity == 0;

  • Make your test in FixedUpdate(), where I believe physics updates are guaranteed to have occurred between calls.

  • If your character cannot move without user input (there is no inertia or drift), you can test for user input.

Comment
Add comment · 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
0

Answer by eweeparker · Aug 03, 2014 at 08:46 PM

Try this:

//Checks to see if player is moving and is on ground if(Mathf.Abs(player.GetComponent(CharacterController).velocity.x) > 1 && player.GetComponent(CharacterController).isGrounded == true) { Debug.Log("you are moving"); }

Comment
Add comment · 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

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

15 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

Related Questions

Mouse as a throttle 1 Answer

Character controls broken due to mouse looking script 1 Answer

space moving physics 1 Answer

Spawn player if player dead 2 Answers

Throw the Player from the Enemy away 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