Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Tolstyy · May 29, 2020 at 02:11 PM · movementcharactercontroller

ClampMagnitude but with different speeds

So im trying to fix my character moving faster on the diagonals and it is still not working and i dont know what i am doing wrong. My character moves also moves slower when going backwards also going sideways. This is what i have tried:

 float axisX = joystickInput.axis.x;
 float axisY = joystickInput.axis.y;
             
 // Speed and axis
 if (axisY < 0) { speed = Mathf.Abs(-axisY) * backSpeed; } // Speed on the negative y-axis       
 else { speed = Mathf.Abs(axisY) * forwardSpeed; } // Speed on the y-axis
 speed += Mathf.Abs(axisX) * sideSpeed; // Speed on the x-axis
 
 // Direction of the VR headset
 Vector3 direction = Player.instance.hmdTransform.TransformDirection(new Vector3(axisX, 0, axisY));
 
 // Fixes faster movement on diagonals
 Vector3 clamp = Vector3.ClampMagnitude(direction, speed);
 
 //Apply
 CharController.Move(Vector3.ProjectOnPlane(clamp, Vector3.up) * speed * Time.deltaTime);
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

1 Reply

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

Answer by viesc123 · May 29, 2020 at 07:01 PM

I like to work visually so I would recommend an AnimationCurve that shows how angle and speed relate. You can tweak this in the Inspector for better results:

     // from 0° = forward to 180° = backwards 
     public AnimationCurve speedByAngle = new AnimationCurve(new Keyframe(0, 1f), new Keyframe(90f, 0.5f), new Keyframe(180, 0.5f));
 
     void MoveController()
     {
         float axisX = joystickInput.axis.x; // assuming this goes from -1 to 1
         float axisY = joystickInput.axis.y; // assuming this goes from -1 to 1
 
         // create a direction vector directly from the input
         Vector3 dir = new Vector3(axisX, 0, axisY).normalized;
 
         // now calculate the angle between forward and your move vector
         // and use this to get the speed from the animation curve
         float speed = speedByAngle.Evaluate(Vector3.Angle(dir, Vector3.forward));
 
         Vector3 global = Player.instance.hmdTransform.TransformDirection(dir);
 
         CharController.Move(global * speed * Time.deltaTime);
     }
Comment
Add comment · Show 2 · 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 Tolstyy · May 29, 2020 at 08:49 PM 0
Share

Worked like a charm! Thank you so much!!

avatar image Tornby · Apr 05 at 11:02 AM 0
Share

The approach using an animation curve is very smart. Thank you for your answer, very helpful.

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

204 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

Related Questions

Top-Down Movement in Unity 1 Answer

8-Axis 3D Top Down Movement 1 Answer

How to Logically Match Ground Slope While Using This Code? 1 Answer

Movement with AddForce: Wrong Direction 2 Answers

How to move to specific point with character controller 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