Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Codinablack · Jan 24, 2018 at 08:14 AM · c#movementplayercontrollervelocity

Return Proper Velocity of Player object

Ok so I am basically trying to create a boolean method isMoving() for a player controller script to be used with an animator. That being said I know there are a few options for this, but I have discovered something quite disturbing, those methods rely completely on how you move the player object. For example:

If I move a player object using

 // agent is our navmesh agent
 agent.destination = destination;

Then I can get the agents velocity and it actually return something other than 0.

However if I move my player object using

 transform.position = Vector3.MoveTowards(lastPosition, targetHit.point, Time.deltaTime * 5.0f);

Then the velocity returns 0;

Same scenario for Rigidbody velocity.

I don't wan't to use the navmesh agent to control my movement for many reasons that aren't relevant to this topic.

My problem is this! I have an idle state, when that idle state is running it changes the x,y,z of the character even though they don't actually move, so, something like this:

 movementSpeed = (transform.position - lastPosition).magnitude / Time.deltaTime * 5.0f;

will spike movement speed up to 4 or 5 just during the animation, the rest of the time it is printing a floating value of something below 0.

I only want isMoving to be true, if the player is actually moving, not just standing idle. I can't figure out what to do here, I have tried taking the x and z cooridinates and absoluting them, adding them, all kinds of math many different ways, but nothing seems to work the way agent.velocity.magnitude worked (perfectly btw!) when I was using agent.destination as the way to move my agent.

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 Codinablack · Jan 24, 2018 at 08:58 AM 0
Share

I need the resulting number, be it float or int or w/e to be used in deciding if the object is moving at all, AND deter$$anonymous$$e the rate of how fast it's moving.

3 Replies

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

Answer by Harinezumi · Jan 24, 2018 at 08:49 AM

You get 0 velocity with Vector3.MoveTowards(), because you don't use physics, you directly assign the return value of MoveTowards() to the position, which is instantaneous movement, without velocity.
Your calculation of movementSpeed is OK, although it can suffer from floating point imprecision. Instead I would recommend to move the animated model onto a child of your player game object, and move the player game object with physics, adding forces or velocity to its rigidbody. This way the animation will not affect your modeling of movement.
A different way to do this is to check if (transform.position - lastPosition).magnitude is below a certain limit, e.g. 0.01f, in which case you consider it to be stopped. Note that you could also use sqrMagnitude and square your limit value as well, which makes things more efficient, but then you can get into floating point imprecision zone again...

Comment
Add comment · Show 6 · 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 Codinablack · Jan 24, 2018 at 08:54 AM 0
Share

Yes I know the reason velocity isn't returned is because I am translating the transform directly. That still feels wrong to me, the velocity of the game object should be able to be found anyways.

What do you mean move the animated model onto a child of my player game object. The player object is the model I'm animating!!!??? I don't understand what you are saying here.

As far as the limit example of 0.01f

That is what I was trying to do, but it's useless when it still spikes to well above 1.0f while standing still.

avatar image Harinezumi Codinablack · Jan 24, 2018 at 09:11 AM 1
Share

What I'm saying is that the animated visual model and the player game object don't have to be the same object. This way you separate the visual representation from the logical and positional representation (I would upload an image example, but it doesn't work :( ).
I means something like this:
- Player (had Collider, Rigidbody, PlayerControl script)
- Visual$$anonymous$$odel (has some kind of Renderer, maybe even $$anonymous$$esh Filter, Animator)

Does this make more sense?
I'm puzzled why your velocity spikes in idle state with that calculation, it really shouldn't, unless lastPosition is not set correctly all the time.

avatar image Codinablack Harinezumi · Jan 24, 2018 at 09:23 AM 0
Share

Ok so what you are saying is if the animator is if I move the animator to a different sub level on the same game object, it won't effect the same transform, yet the animator will still work as intended, thus eli$$anonymous$$ating the movements of the animator in the calculations of the transform for the root?

It spikes during the biggest movement, and yes I am setting the last position correctly, see other replies comments for fixed update method I am using.

Another interesting fact, rigidbody velocity doesn't actually stay 0 it randomly generates a number without me doing anything. 0,0,0,0,0, 1639, 0,0,0,0 1639, 0,0 something like that... weird...

Show more comments
avatar image
1

Answer by Pangamini · Jan 24, 2018 at 08:54 AM

If you don't want to use rigidbody, you can use the (transform.position - lastPosition).magnitude approach, but I'd strongly suggest you to do that in some fixed step (and therefore constant deltaTime) to avoid floating point inprecision fluctuations. You can use the FixedUpdate for that purpose. Just remember to store the lastPosition in the same fixed update as well.

Comment
Add comment · Show 5 · 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 Codinablack · Jan 24, 2018 at 08:57 AM 0
Share

Did you read the entire thing.... I said I did that approach. It doesn't work for me! It doesn't work because it spikes too high, and changes with IDLE animation.

avatar image Pangamini Codinablack · Jan 24, 2018 at 09:02 AM 0
Share

Well I don't see you mentioning fixed delta time / update, but maybe I am just missing it somehow

avatar image Codinablack Pangamini · Jan 24, 2018 at 09:16 AM 0
Share

Ok I don't specify where I used that approach. I am sorry for being snippy when you are trying to help, but man all you did was recommend I try to use an approach that I already stated doesn't work for me and why it doesn't work for me. Yes i used that approach correctly my Fixed update looks like this

 void FixedUpdate()
 {
     if(!isLocalPlayer)
     return;
     movementSpeed = (transform.position - lastPosition).magnitude / Time.deltaTime * 5.0f;


     
     lastPosition = transform.position;

     if (Pressed$$anonymous$$ovementButton() || Holding$$anonymous$$ovementButton())
     {
         target.IsSelected = false;
         if (Holding$$anonymous$$ovementButton())
         {
             Control$$anonymous$$ovementWithButtons();
         }
     }
 }
Show more comments
avatar image Pangamini Codinablack · Jan 24, 2018 at 11:41 AM 1
Share

use Time.fixedDeltaTime when in FixedUpdate(). That's all you need to stop the spikes.

avatar image
0

Answer by Codinablack · Jan 24, 2018 at 10:59 PM

Anybody else out there got some clever ideas on how to make this work correctly?

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

453 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

How can i smooth out the rotation? 1 Answer

How to make a good PlayerMovement 1 Answer

Error CS0030 Cannot convert type `UnityEngine.Networking.PlayerController' to `PlayerController123',CS0030: Space Shooter Tutorial 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