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 BitMorsel · Jun 27, 2013 at 07:41 AM · axisforceaxes

Inhibit force from doubling when 2 axes are active

I'm using a rolling ball controller from the Circular Force Package found on the asset store. My problem is when I move the ball in a diagonal direction, the force becomes doubled when both axes are active. I can't figure out how to inhibit the force while both are active. I tried dividing the force by 2 when both are active, but that didn't give the best result. Any suggestions are greatly appreciated.

Here's part of the code...

     void FixedUpdate()
     {
         //Right Left controls
         float horMovement = movementForce * Input.GetAxis("Horizontal");
         
         //Forward Backward controls
         float verMovement = movementForce * Input.GetAxis("Vertical");
         
         //Up Down controls. Note: For Zero G 
         float floatMovement = movementForce * Input.GetAxis("Mouse ScrollWheel");
         
     
            if (horMovement != 0)
         {
             this.transform.rigidbody.AddForce(new Vector3(horMovement, 0, 0), ForceMode.Impulse);    
         } 
         
         else
    
          if (verMovement != 0)
         {
             this.transform.rigidbody.AddForce(new Vector3(0, 0, verMovement), ForceMode.Impulse);
         }
     
         
         
         if (floatMovement != 0)
         {
             this.transform.rigidbody.AddForce(new Vector3(0, floatMovement * 4, 0), ForceMode.Impulse);
         }
     }
 
     //Enables/Disable the circular gravity
     private void EnableCircularGravity(bool enable)
     {
         CircularGravity circularGravity = this.GetComponent<CircularGravity>();
 
         circularGravity.enable = enable;
     
     }
     
     
     
     
     
     
     
 }

 
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
0

Answer by Bunny83 · Jun 27, 2013 at 07:58 AM

Just use Vector3.ClampMagnitude like this:

 void FixedUpdate()
 {
     Vector3 moveDirection = new Vector3(
         Input.GetAxis("Horizontal"),
         0,
         Input.GetAxis("Vertical")
     );
     moveDirection = Vector3.ClampMagnitude(moveDirection,1.0f);
     moveDirection.y = Input.GetAxis("Mouse ScrollWheel")*4;
     rigidbody.AddForce(moveDirection * movementForce, ForceMode.Impulse);
 }

I'm not sure why you use ForceMode.Impulse since it's ment for one-time boosts relative to the objects mass. You shouldn't do this every frame. I would probably do something like:

 void FixedUpdate()
 {
     Vector3 moveDirection = new Vector3(
         Input.GetAxis("Horizontal"),
         0,
         Input.GetAxis("Vertical")
     );
     
     moveDirection = Vector3.ClampMagnitude(moveDirection,1.0f);
     Vector3 upMovement = Vector3.up * Input.GetAxis("Mouse ScrollWheel")*4;
     
     rigidbody.AddForce(moveDirection * movementForce, ForceMode.Acceleration);
     
     if (upMovement != 0.0f)
         rigidbody.AddForce(upMovement * movementForce, ForceMode.Impulse);
 }
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 BitMorsel · Jun 27, 2013 at 08:09 AM 0
Share

Thanks a bunch! That worked great!

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

16 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

Related Questions

xsi to fbx model has inverted axis 0 Answers

Creating an axes representation on the screen? 0 Answers

Jumping with force in forward direction 2 Answers

Get raw joystick (axes) input 0 Answers

How to determine the axis specific speed (or velocity) of a kinematic object? 0 Answers


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