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 GliconCraft · Jan 23, 2018 at 01:15 AM · addforcemath

Unity AddForce Problem (not a bug problem, but a math problem)

Sorry for the lack of details in the title, the reason for that is that I don't know a short way of how to explain the problem I want to solve. Here's some context: I'm making a 3D platformer game where my player is a sphere that rolls and jumps around. I've already got in an orbital camera, the controls work fine and everything, but the method I use to move my player around is AddForce.

Problem:

This becomes a problem when I hold down both the W and the D key, it's applying the same speed unit to both the x and the z axis, which is actually causing my sphere to move faster diagonally rather than laterally. I've included a bit of a diagram at the bottom of what's happening versus what I'm trying to get to happen:

For the purposes of simplicity, let's only add forces of 1 unit. Let the green arrow represent the force being applied. alt text

unity-addforce-problem.png (137.5 kB)
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 · Jan 23, 2018 at 01:36 AM

Uhm you have to normalize your force vector. Or if you want to have smaller values you may want to use Vector3.ClampMagnitude with a max length of 1f. This will make the vector to be clamped to the length of 1 only if it's larger.

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 Buckslice · Jan 23, 2018 at 01:40 AM 0
Share

@Bunny83 you always beat me by seconds! Ya this is the better answer though, I'll leave $$anonymous$$e up as an additional random option.

avatar image Bunny83 Buckslice · Jan 23, 2018 at 01:49 AM 0
Share

:) I'm sorry but it happens to me all the time as well as i tend to write quite long and detailed answers which often leads to co$$anonymous$$g late to the party ^^.

avatar image GliconCraft · Jan 23, 2018 at 01:42 AM 0
Share

@Bunny83 Thanks for that information, after thinking about it, I realized this has to be a problem that's been noticed before, so I went searching for an answer, googling "moves faster diagonally" and surely enough, it's been answered. Although, I'm having a bit of a problem with that answer, because (I haven't tested it yet) but wouldn't Vector3.Clamp$$anonymous$$agnitude not only clamp it on the x and z axis, but also on the y axis? How could I exclude that? $$anonymous$$y game utilizes gravity and jumping as a platformer, so I don't want to limit the fall speed. Thanks for your response!

avatar image Bunny83 GliconCraft · Jan 23, 2018 at 01:53 AM 1
Share

Uhm, wasn't the question about a force vector which you constructed from the input? I didn't suggest to clamp the velocity. Of course when you want to clamp the horizontal velocity you would have to decompose the velocity into a horizontal and vertical part, clamp the horizontal part and combine them back into a single velocity. Though as i said your question was about a force vector.

avatar image GliconCraft Bunny83 · Jan 23, 2018 at 02:02 AM 0
Share

Sorry, I might not be explaining it properly, I thought that Vector3.Clamp$$anonymous$$agnitude would clamp the velocity, but perhaps I'm wrong. x) I've never really used it, I'll take a look at it on the wiki and try it out in my game and see if I encounter any issues and then go from there. Either way, thanks!

avatar image
2

Answer by Buckslice · Jan 23, 2018 at 01:38 AM

alt text

What you should do is detect when there is both Vertical and Horizontal input and then instead do rb.AddForce(0.7071, 0, 0.7071)


root2tri.gif (3.8 kB)
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 GliconCraft · Jan 23, 2018 at 01:46 AM 0
Share

@Buckslice Thanks! This helps me exclude velocity resulting from gravity from being clamped, although that doesn't seem like the simplest way to manage it, but I probably will use that until I figure out how to exclude the y from being clamped.

avatar image Buckslice GliconCraft · Jan 23, 2018 at 01:52 AM 0
Share

Ya my method is basically manually normalizing it. Not sure exactly how your code is setup, but if you want to normalize but ignore the y component of the vector you could do something like this

 float ySave = velocity.y;
 velocity.y = 0.0;
 velocity.Normalize();
 velocity.y = ySave;

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

79 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

Related Questions

addForce to Character in direction of other object 1 Answer

Achieving consistent jump height regardless of vertical velocity 2 Answers

Movement with AddForce: Wrong Direction 2 Answers

Adding rotation to Physics.AddForce(); ... Getting weird error? 0 Answers

Add Force to the right of the rigidbody, not right of the screen 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