• 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 TinyUtopia · Apr 13, 2010 at 04:10 PM · rigidbodyverticalhorizontaladdrelativeforce

limit AddRelativeForce

I am using AddRelativeForce to move an object.

The problem is that when both Input.GetAxis ("Vertical") and Input.GetAxis ("Horizontal") are pressed, the object travels at the rate of 2.0 overall. I would like to limit the overall AddRelativeForce to 1, even if both directions are pressed. In other words, if both Input.GetAxis ("Vertical") and Input.GetAxis ("Horizontal") are pressed, then I want each of them to influence the object with a value of .5 so that the overall combined value is clamped at 1.0

var force = Input.GetAxis ("Vertical");
var torque = Input.GetAxis ("Horizontal");
rigidbody.AddRelativeForce (torque, 0, force);
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
1
Best Answer

Answer by duck · Apr 13, 2010 at 04:28 PM

This question is slightly confusing since you're using the term "Torque" (which relates to rotation) when you seem to be actually using it for linear force. However, ignoring that, it seems like what you want is to "normalize" the lengths of your horizontal and vertical forces so that the total "length" of the vector doesn't exceed 1.

This is not the same as making them add up to 1 - because if an object is moving at a speed of X:0.5 and y:0.5, it would actually be moving slower than an object that is moving at x:1, y:0. Normalizing the values as a vector will give you the results that I think you want (although please correct me if I have the wrong idea).

Since another version of AddRelativeForce accepts a Vector3 (as opposed to separate x,y,z values), you can make use of the normalization functions that unity has built into its Vector3 class:

var v = Input.GetAxis ("Vertical"); var h = Input.GetAxis ("Horizontal"); var relativeForce = Vector3(h, 0, v);

if (relativeForce.magnitude > 1) { relativeForce.Normalize(); }

rigidbody.AddRelativeForce(relativeForce);

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 TinyUtopia · Apr 13, 2010 at 04:46 PM 0
Share

Great Duck. Yes, even with my mishandled description you understood exactly what I was trying to do, thanks.

avatar image TinyUtopia · Apr 13, 2010 at 04:56 PM 0
Share

Is there any way to get a variable to normalize at a value other than 1?

avatar image TinyUtopia · Apr 13, 2010 at 05:13 PM 0
Share

Just out of curiosity, what would be the forward velocity of an object moving at X:0.5 and y:0.5?

avatar image duck ♦♦ · Apr 13, 2010 at 06:40 PM 0
Share

"normalize at a value other than 1?" yes - normalize it to 1, then multiply by whatever value you want! :)

avatar image duck ♦♦ · Apr 13, 2010 at 06:43 PM 0
Share

The velocity of something moving at x:0.5 y:0.5 would be about 0.707 - for more information, google pythagoras!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

AddForce on GetAxis always sam direction? 1 Answer

How to have vertical and horizontal axis work at the same time 1 Answer

How can I make GUI button send an input message when I press it? 0 Answers

Rigidbodies with fixed joint connection rotate instead of forward/back 1 Answer

How to apply a radial force 2 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