Question by
faizanmir009 · May 20, 2020 at 08:16 PM ·
rigidbodyvelocityforcetorque
Torque applies to one wheel, but inconsistently to the other wheel
I have a GameScript class which is derived by another class WheelScript; it is meant to apply torque to a rigidbody in different directions depending upon the key pressed.
However the result to this is that one of the rigidbodies rotates fine but the other one doesn't (say the right wheel rotates but the left doesn't turn or even if it does, it doesn't move with the same angular velocity as the first one)
Base Class is as
public class GameScript : MonoBehaviour
{
public static GameScript instance;
protected float Force;
protected virtual void Print(GameObject gameObject) {
print(gameObject.name);
}
protected virtual void MoveForward(Rigidbody rigidBody, int identifier) {
if(identifier == 1)
{
rigidBody.AddRelativeTorque(-Vector3.forward * Force);
}
else
{
rigidBody.AddRelativeForce(-Vector3.forward * Force);
}
}
protected virtual void MoveLeft(Rigidbody rigidBody, int identifier) {
if (identifier == 1)
{
rigidBody.AddRelativeTorque(-Vector3.forward * Force);
}
else
{
rigidBody.AddRelativeForce(-Vector3.forward * Force);
}
}
protected virtual void MoveRight(Rigidbody rigidBody, int identifier) {
if (identifier == 1)
{
rigidBody.AddRelativeTorque(Vector3.forward * Force);
}
else
{
rigidBody.AddRelativeForce(-Vector3.forward * Force);
}
}
protected virtual void MoveBack(Rigidbody rigidBody, int identifier) {
if (identifier == 1)
{
rigidBody.AddRelativeTorque(-Vector3.forward * Force);
}
else
{
rigidBody.AddRelativeForce(-Vector3.forward * Force);
}
}
}
Child Class is as :
public class WheelScript : GameScript
{
[SerializeField] private int identifier;
Rigidbody body;
[SerializeField] float force;
private void Start()
{
body = GetComponent<Rigidbody>();
Force = force;
}
void Update()
{
if (Input.GetKey(KeyCode.W))
{
MoveForward(body, identifier);
}
else if (Input.GetKey(KeyCode.A))
{
MoveLeft(body, identifier);
}
else if (Input.GetKey(KeyCode.D))
{
MoveRight(body, identifier);
}
else if (Input.GetKey(KeyCode.S))
{
MoveBack(body, identifier);
}
}
}
The Hinges are attached as shown in the picture:
Hinges point in opposite direction as (0,0,1) and (0,0,-1) (Axis of the hinge with anchor at (0,0,0) which is the pivot of the T-shaped body
for further reference please read this link text
upload-4.png
(287.5 kB)
Comment