- Home /
Problem with addforce angle or mouse input
So this is what I have so far
Quaternion rotationr = Quaternion.Euler(30, 0, 0);
Vector3 directionr = rotationr * transform.forward;
Quaternion rotationl = Quaternion.Euler(-30, 0, 0);
Vector3 directionl = rotationl * transform.forward;
float mouseX = Input.GetAxis("Mouse X");
if (mouseX > 0 && grnded == false)
{
player.AddForce(directionr * airturnmom * mouseX * Time.deltaTime);
}
if (mouseX < 0 && grnded == false)
{
player.AddForce(directionl * airturnmom * mouseX * Time.deltaTime);
}
It mostly works and turning right in the air gives the right force, when I turn left though it gives the opposite of the right one though. I've tried a bunch of stuff and it definitely seems like something is set up wrong somehow.
Answer by Captain_Pineapple · Mar 03 at 03:54 PM
Hey there,
first up please try to keep your code clean and easy to maintain:
float mouseX = Input.GetAxis("Mouse X");
Quaternion rot = Quaternion.Euler(30*Mathf.Sign(mouseX), 0, 0);
Vector3 dir = rot * transform.forward;
if (!grnded)
{
player.AddForce(dir * airturnmom * mouseX * Time.deltaTime);
}
This code above does the same thing as your current code and each thing that you have to change if something should be changed can be done with 1 change exactly. If your code has the almost the exact line twice after each other then things are not right.
Apart from that it is kind of weird that the right turn is working as you turn the forward vector around the x-axis, not the y-axis. But given your info that it indeed does work then the only thing that probably causes this issue is the fact that you multiply your dir
(formerly directionl
) with the mouseX
value. mouseX
goes negative which turns the vector 180°.
So what you probably want to do here is "only" scale the vector as the rotation has already been done:
player.AddForce(dir * airturnmom * Mathf.Abs(mouseX) * Time.deltaTime);
Let me know if that helped.
Answer by LV9999_Majin · Mar 03 at 08:41 PM
Wow yeah, that worked perfectly. Still like 100 other problems with the movement, that's doing what it's supposed though. Thanks a ton.
Also pretty sure there's no chance I'm keeping any code clean yet. I'm literally just trying to mangle random things in from the unity descriptions as I actually figure out what's going on.
Your answer

Follow this Question
Related Questions
How to rotate a character facing direction of movement like sonic the hedgehog 2 Answers
Move object towards an angle the distance of two points 1 Answer
More Efficient Way to Move Object at Angle 0 Answers
How to rotate Character depending on the inclination of the platform or its parts? 1 Answer
Bounce into the air 1 Answer