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 Arustyred · Jul 20, 2017 at 07:51 PM · animationunity 5movementvector3issue

[Solved] Issues knowing if an object is moving "forward" or "backwards" (C#)

Hello. I'm trying to have a character play a different animation when they move forwards, backwards, left, and right. in my code, i use

Vector3 localVelocity = transform.InverseTransformVector (_velocity);

where _velocity is the amount the player moves each update.

When I use localVelocity.z to get the forward velocity, I get 6 and negative 6 when i go forwards and backwards, which is correct. The problem is when I move left and right, I sometimes get values like 1.73265 or 2.736 or other weird numbers. This number goes into this part of my script,

if (localVelocity.z != 0) {

 anim.SetBool ("isWalking", true);
 Debug.Log ("true");

} else {

 anim.SetBool ("isWalking", false);
 Debug.Log ("false");

}

and plays a forward animation when i'm moving sideways. Any idea why this happens, and how to fix this?

I have also tried setting localVelocity equal to transform.InverseTransformDirection, and transform.InverseTransformPoint, but I get the same problem.

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

2 Replies

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

Answer by Bunny83 · Jul 20, 2017 at 10:16 PM

The problem is that you're dealing with floating point values. Using ">0" and "<0" or "!=0" is never a good idea. The values you get is probably not "1.73265" but something like "1.73265E-10" so the actual number would be "0.000000000173265" which is "almost" 0

You have to test against a threshold:

 if (Mathf.Abs(localVelocity.z) > 0.01f)
 {
     anim.SetBool ("isWalking", true);
    // ...

or like this which would do the same:

 if (localVelocity.z > 0.01f || localVelocity.z < -0.01f)
 {
     anim.SetBool ("isWalking", true);
    // ...
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 Arustyred · Jul 21, 2017 at 02:28 AM 0
Share

Thank you, that helped a lot! :D

avatar image
0

Answer by rageingnonsense · Jul 20, 2017 at 08:30 PM

I think you are using the wrong method in the firstplace. InverseTransformVector() transforms a vector from world to local space. This will not give you the data you want. All this is doing is converting your _velocity to local space

If you want to know where you are relative to where you were, you can use subtraction:

 public Vector3 oldPosition;

 void FixedUpdate() {
     Vector3 direction = transform.position - oldPosition;
     float forwardTest = Vector3.Dot(-direction.normalized, transform.position.normalized)
 
     if(forwardTest  > 0) {
         // forward
     } else if (forwardTest < 0) {
        // backward
     } else {
        // neither
     }
 
     float sideTest = Vector3.Dot(direction.normalized, Vector3.Cross(transform.forward, transform.up).normalized) ;
     if(sideTest > 0) {
         // right (maybe left?)
     } else if (sideTest < 0) {
         // left (maybe right?)
     } else {
         //neither
     }

     oldPosition = transform.position;
 }

I forget which result will be right or left in the second test.

This will tell you raw direction. if your forward test is 1, and your side test is 0, you are just walking straight. If your side test is 1, and your forward test is 0, you are strafing. If you have a combination, then you are strafing while moving forward or back. If they are both 0 (or close to it), you didn't move.

This is the general idea. Dot product of two normalized vectors gives a direction. The sign of the result tells you if you are forward or back (I might have swapped them).

The side test is similar, we just re-check it with a perpendicular vector created via a cross product.

There might be a simpler way to do this, but this is what came to mind.

EDIT: This is a handy tool to help understand how dot products work, and why it is useful for you: http://www.falstad.com/dotproduct/

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 Bunny83 · Jul 20, 2017 at 10:11 PM 0
Share

Uhm sorry but the code the OP uses is actually right while this line makes no sense:

 float forwardTest = Vector3.Dot(-direction.normalized, transform.position.normalized)

This doesn't test the direction you're moving but if you move away from the world origin or if you get closer to the world origin. When "forwardTest" is greater than 0 you actually move towards the world origin and when it's smaller than 0 you move away. $$anonymous$$aybe you wanted to use "transform.forward" ins$$anonymous$$d of transform.position?

Transfor$$anonymous$$g the world-space velocity into local space does give you the information how fast you move in each local axis.

Though i would have used InverseTransformDirection ins$$anonymous$$d of "InverseTransformVector" as you usually don't really care about the scale.

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

237 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

Related Questions

¿How to use animation for same object at different positions? 2 Answers

How to implement animation into a click to move script plzzzzzzz 0 Answers

root motion on in place animation 0 Answers

Players can't move through objects, but they can glitch through it. 1 Answer

Issues moving rigidbody with animation(C#) 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