- Home /
The question is answered, right answer was accepted
Keep current force applied based on Vector3.forward?
Hey, first post on here, I'm curious about how I would achieve this. What I have setup currently is a cube that moves forwards, and rotates using addforce and addtorque. What I'd like to achieve is when there is a current force that's applied to the game object (When a vertical inpuit is held) and the cube is then rotated; the force will continue on based on the front of the cube. Rather than just slide towards where the previous force was pushing to. Similar to how a car would move.
Excuse this rough drawing, but a visual representation may help convey the idea.
The code I currently am using is this.
rb.AddForceAtPosition((Input.GetAxis("Vertical") * forwardForce * transform.forward), transform.position + addForceLocation, ForceMode.Force);
rb.AddRelativeTorque(Vector3.up * (Input.GetAxis("Horizontal") * turnForce), ForceMode.Force);
Add a forward force when the vertical input is pressed, times by the desired force in a forward direction, in the root of the rigid body plus the offset.
Rotate torque on the Y axis, by the desired force times by the horizontal input
Answer by Tripal · Jun 09, 2018 at 02:28 PM
Found a solution to this for anyone else that comes across this later on. Grip: 0, meaning no traction - 100 decent amount of traction.
public float grip = 100f;
Vector3 turningGrip = (Quaternion.Inverse(transform.rotation))* rb.velocity;
turningGrip.x = Mathf.Lerp(turningGrip.x, 0, Time.deltaTime * grip);
rb.velocity = transform.rotation * turningGrip;
Follow this Question
Related Questions
Torque applied only after force 0 Answers
AddForce Movement Constraint 0 Answers
Animation stops rotation 0 Answers
How to move a rigidbody along a curve or spiral 0 Answers
Smooth movement along an uneven surface 2 Answers