2D Racer Game

02: Initial Player Setup

 Add a car sprite to your track (place the car sprite on layer 2)

Add a capsule 2D collider and use the edit collider button to fit it to your car.

Screen Shot 2017 11 23 at 14.03.40

 Now add an AudioSource component, and a rigidBody 2D component. Set the Gravity Scale to 0 on the 2D rigidbody.

 

Create a new script. Call it "Drive" and attach it to the car.

add the following script to get the car to race. Set the variables in the inspector (you may need to adjust setting for your car size and track design). Also add an engine noise to the audio source and play on awake / loop.

Screen Shot 2017 11 23 at 14.21.36


JS

CURRENTLY NOT AVAILABLE

C#

using UnityEngine;

public class Drive : MonoBehaviour {

    public float rotateSpeed;
    float Speed;
    public float acc;
    public float topSpeed;
    float maxSpeed;
    float audioClipSpeed;
    AudioSource AS;
    bool startRace;

    // Use this for initialization
    void Start () {

        Speed = 0.0f;
        maxSpeed = topSpeed;
        audioClipSpeed = 10.0f;
        AS = GetComponent<AudioSource> ();
        startRace = false;
        //StartCoroutine(getSetGo());
        
    }
    
    // Update is called once per frame
    void FixedUpdate () {

        //CAR SOUND PITCH INCREASES AS CAR MOVE FASTER //
        float p = Speed * audioClipSpeed;
        AS.pitch = Mathf.Clamp( p, 0.1f, 1.0f);

        transform.Rotate (0, 0, Input.GetAxis ("Horizontal") * rotateSpeed * Speed * Time.deltaTime);


        /// ACCELERATION ///
        if (Input.GetAxis ("Vertical") == 1 && Speed < maxSpeed) {
            Speed += acc;
        } else if (Speed > 0) {
            Speed -= acc;
        }

        if (Input.GetAxis ("Jump") == 1 && Speed > 0) {
            Speed -= (acc * 4);

        }

        transform.Translate (0, Input.GetAxis ("Vertical") * Speed * Time.deltaTime, 0);

        
    }
}

 

Casino Bet 365 is best casino in the world.

What's Next

Next up for Unity Snippets is a look at game development articles from across the web...

Free Templates - bigtheme.net
Popular Art Betting make bookies articles.