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 ArminAhmadi · May 07, 2019 at 05:47 PM · transformdirection

How much the transform moved in it's directions

I have a character with an animator attached to it. Animator has two parameters (horizontal and vertical) which controls strafe right/left and move forward/backward. So I'm trying to set the animator parameters values based on how much characters transform moved in x and z direction. I use this code :

     private Vector3 lastPosition;
     private float vertical;
     private float horizontal;
     public bool run;
     public Animator animator;
     private void Update()
     {
         horizontal = (transform.position.x - lastPosition.x);
         vertical = (transform.position.z - lastPosition.z);
         float max = Mathf.Max(Mathf.Abs(horizontal), Mathf.Abs(vertical));
         if (max == 0)
         {
             horizontal = 0;
             vertical = 0;
         }
         else
         {
             horizontal /= max;
             vertical /= max;
         }
         if (!run)
         {
             horizontal /= 2;
             vertical /= 2;
         }
         animator.SetFloat("vertical", vertical);
         animator.SetFloat("horizontal", horizzontal);
         lastPosition = transform.position;
     }

So the code works when character facing (0, 0, 1) direction but as soon as I rotate the character, thing get messed up. By the way I can't use input axis to set the animator parameters so don't suggest that, because the camera is not always behind the character and it can be rotated around character freely. So any ideas?

Comment
Add comment · Show 2
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 Romano · May 08, 2019 at 10:41 AM 0
Share

Whenever I have any problems with rotations and position I usually try seeing what using localPosition does ins$$anonymous$$d.

For example, try changing

 horizontal = (transform.position.x - lastPosition.x);

to

horizontal = (transform.localPosition.x - lastPosition.x);

You would also have to make sure lastPosition is also based on a local position and not a world position.

avatar image DCordoba · May 08, 2019 at 11:18 AM 0
Share

please check where you are updating lastPosition at least, update it on the Update function...

  void Start(){
        lastPosition = transform.position;
  }

  private void Update()
  {
      horizontal = (transform.position.x - lastPosition.x);
      vertical = (transform.position.z - lastPosition.z);
      float max = $$anonymous$$athf.$$anonymous$$ax($$anonymous$$athf.Abs(horizontal), $$anonymous$$athf.Abs(vertical));
      lastPosition = transform.position;
      ...

0 Replies

· Add your reply
  • Sort: 

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

138 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

Related Questions

How to get the direction my Steam VR controller is pointing? 0 Answers

Raycast between TransformDirection and Vector3.Forward? 1 Answer

Difference between Vector.up and transform.up 2 Answers

Shooting script C# 1 Answer

transform.TransformDirection() returns NaN 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