- Home /
How To Accelerate/Decelerate Gradually With Torque GetAxis
Hi guys, I'm really sorry to be doing this but I'm really lost and I've been searching Google and Unity Answers for a couple of weeks for the answer to this problem.
Basically I have a ball rolling game that I apply torque to move the player, it's using GetAxis Horizontal and Vertical, which I would have thought should have been easy to do this but everything I find on AddTorque or even AddForce for acceleration appears to only work for GetKey.insert arrow keys here and so-forth.
I'm needing a solution that uses GetAxis that essentially adds a good inertia to my acceleration and deceleration in any angle the axis can be pointed in at any given time.
Here is what I have, sorry I don't have a half-baked solution in there somewhere, I'm literally at a loss:
void FixedUpdate()
{
if (onGround)
{
Vector3 torqueVector = (Input.GetAxis("Horizontal") * -mainCamera.transform.forward) + (Input.GetAxis("Vertical") * mainCamera.transform.right);
GetComponent<Rigidbody>().AddTorque(torqueVector.normalized * accRate * Time.deltaTime, ForceMode.Acceleration);
}
else
{
dir.x = ((Input.GetAxis("Vertical") * flySpeed) * Time.deltaTime);
dir.z = ((Input.GetAxis("Horizontal") * flySpeed) * Time.deltaTime);
GetComponent<Rigidbody>().velocity += (mainCamera.transform.right * dir.z + new Vector3(mainCamera.transform.forward.x, 0, Camera.main.transform.forward.z) * dir.x);
Vector3 torqueVector = (Input.GetAxis("Horizontal") * -mainCamera.transform.forward) + (Input.GetAxis("Vertical") * mainCamera.transform.right);
GetComponent<Rigidbody>().AddTorque(torqueVector.normalized * accRate, ForceMode.Acceleration);
}
}
The 'else' part is for when it's in the air, the player is allowed to AddForce so they can move slightly in the air while also able to manipulate torque.
I hope one of you brilliant people out there can help me out, I'd be greatly appreciative!
Answer by centaurianmudpig · Nov 25, 2015 at 05:32 PM
I've done something similar, for rotating an object, using AddRelativeTorque based on mouse/keyboard input.
float myDesiredSpeed = maxYawSpeed * yawRotation
parentRigidbody.AddRelativeTorque(0, myDesiredSpeed * Mathf.Rad2Deg, 0, ForceMode.Impulse)
yawRotation is a value between 0 and 1.0, taken from the joystick/mouse. maxRotSpeed is the maximum rotation speed in Radians Per Second.
As you can guess this only works on a single rotation and you can add another axis easy enough or however you like.
Hey thanks for taking the time to answer!
I'm stuck again though, how can I have my vector3 input for axis be turned into a float?
Here is what I have so-far:
Vector3 torqueVector = (Input.GetAxis("Horizontal") * -mainCamera.transform.forward) + (Input.GetAxis("Vertical") * mainCamera.transform.right);
float accRate = maxYSpeed * torqueVector;
GetComponent<Rigidbody>().AddTorque(0, accRate * $$anonymous$$athf.Rad2Deg, 0, Time.deltaTime, Force$$anonymous$$ode.Acceleration);
Input.GetAxis() is a float. Use it as is, rather than converting it to a vector.
Alright thanks, but how do I do that though?
I need to have this line work the same after using it as a float:
Vector3 torqueVector = (Input.GetAxis("Horizontal") * -mainCamera.transform.forward) + (Input.GetAxis("Vertical") * mainCamera.transform.right);
Use variables to store the input.
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Then use the variables as needed.
Thanks, I'm still not quite getting it though, what am I doing wrong here:
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
float accRate = maxSpeed * horizontal;
GetComponent<Rigidbody>().AddTorque(0, accRate * -mainCamera.transform.forward * $$anonymous$$athf.Rad2Deg, 0, Force$$anonymous$$ode.Acceleration);
float accRate2 = maxSpeed * vertical;
GetComponent<Rigidbody>().AddTorque(0, accRate2 * -mainCamera.transform.right * $$anonymous$$athf.Rad2Deg, 0, Force$$anonymous$$ode.Acceleration);
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Distribute terrain in zones 3 Answers
Can't get character to Jump on the Y Axis (C#) 2 Answers
Simple 2-Button-Mash script 1 Answer
Rotating a player object without rotating the axis 0 Answers