- Home /
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.
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.
@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.
:) 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 ^^.
@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!
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.
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!
Answer by Buckslice · Jan 23, 2018 at 01:38 AM
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)
@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.
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
Follow this Question
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