1
\$\begingroup\$

I am working on a 2D driving game.I have a button(forward).I need to move the vehicle by pressing on the button. When the button is pressed the vehicle should accelerate upto its maximum speed and when the button is release the vehicle should gradually slow down.

float rotateSpeed=0.1f;
float speedForce = 15f;
float torqueForce = -200f;
float driftFactorSticky = 0.9f;
float driftFactorSlippy = 1;
float maxStickyVelocity = 2.5f;
float minSlippyVelocity = 1.5f;

bool moveForward=false;
bool moveBackward=false;
bool turnLeft=false;
bool turnRight=false;

Vector3 myRot;
void FixedUpdate () {
    Rigidbody2D rb = GetComponent<Rigidbody2D>();
    float driftFactor = driftFactorSticky;
    if(RightVelocity().magnitude > maxStickyVelocity) {
        driftFactor = driftFactorSlippy;
    }
    rb.velocity = ForwardVelocity() + RightVelocity()*driftFactor;


    //Move rover forward and backward 
    if(Input.GetKey(KeyCode.UpArrow) || moveForward==true ||  Input.GetButton("Accelerate") ) {
        Debug.Log ("Uparrow....................................................");
        rb.AddForce( transform.up * 0.3f);

    }
    if(Input.GetKey(KeyCode.DownArrow) || moveBackward==true || Input.GetButton("Brakes")  ) {
        Debug.Log ("break....................................................");

        rb.AddForce( transform.up * -speedForce/30f );


    }
    float tf = Mathf.Lerp (0, torqueForce, rb.velocity.magnitude / 2);
    rb.angularVelocity = Input.GetAxis ("Horizontal") * tf;

    if (Input.GetKey (KeyCode.RightArrow) || turnRight==true) {

        transform.Rotate(0,0,-0.5f);
    }
    if (Input.GetKey (KeyCode.LeftArrow) || turnLeft==true) {
        transform.Rotate(0,0,0.5f);

    }

}

Vector2 ForwardVelocity() {
    return transform.up * Vector2.Dot( GetComponent<Rigidbody2D>().velocity, transform.up );
}

Vector2 RightVelocity() {
    return transform.right * Vector2.Dot( GetComponent<Rigidbody2D>().velocity, transform.right );
}

this is working fine but the problem is that when I release the forward button the speed of vehicle is not getting slow down.

Can anybody help me please

\$\endgroup\$
  • \$\begingroup\$ Have you played with the linear drag parameters on the rigidbody? \$\endgroup\$ – DMGregory Oct 4 '16 at 5:45
  • \$\begingroup\$ @DMGregory : No,I havent.The linear drag is set to 0 \$\endgroup\$ – user1509674 Oct 4 '16 at 6:08
  • \$\begingroup\$ Considering you want to have drag, maybe you should add some linear drag to your rigidbody. \$\endgroup\$ – jgallant Oct 4 '16 at 9:50
  • 1
    \$\begingroup\$ @jgallant : Its working. I have set the liner drag to 1 \$\endgroup\$ – user1509674 Oct 4 '16 at 11:13
  • \$\begingroup\$ Not directly linked to the question, but if you work on a racing game you will enjoy this read : gamasutra.com/view/feature/134845/… \$\endgroup\$ – realUser404 Oct 4 '16 at 12:14
1
\$\begingroup\$

To make the vehicle slow down, you can use physics properties like drag and friction, or you can add a negative force (less strong than braking).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.