🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Scripts don't work as expected,

Started by
0 comments, last by faizanmiir 4 years, 1 month ago

Ok So,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 rigid body rotates fine but the other one doesnt (Say the right wheel rotates but the left doesnt turn or even if it does ,it doesnt move with the same angular velocity as the first one) I am posting my code here ,Please Help

Base Class:

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:

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);
    }
}
}

Stack Link : https://gamedev.stackexchange.com/questions/182777/torque-applies-to-one-wheel-but-inconsistently-to-the-other-wheel

This topic is closed to new replies.

Advertisement