Hi Everyone I am new to unity and c#. I am creating a 2D racing game like hill Climb. Can anybody help With this! All I need to do is to add game over scene when the car goes upside down and when fuel is empty. Waiting for Reply PLS HELP! I Had attached the car controller c# Script Below
THANK YOU!
Regards,
MTSK
@Bunny83 @Eric5h5 @robertbu @aldonaletto @tanoshimi @whydoidoit @duck @fafase @Statement @Mike 3
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class carcontroller : MonoBehaviour {
public Rigidbody2D carRigidBody;
public Rigidbody2D backTire;
public Rigidbody2D frontTire;
public float speed = 20;
public float carTorque = 10;
private float movement;
public float gas = 1f;
public float gasConsumption = 0.1f;
public Image gasImage;
[SerializeField]
private Text coinCounter;
private int collidedCoinValue;
private int moneyAmount;
// Start is called before the first frame update
void Start()
{
GetComponent<Rigidbody2D>();
moneyAmount = 0;
}
// Update is called once per frame
void Update(){
movement = Input.GetAxis("Horizontal");
coinCounter.text = " " + moneyAmount;
gasImage.fillAmount = gas;
}
private void FixedUpdate()
{
if (gas > 0)
{
backTire.AddTorque(-movement * speed * UnityEngine.Time.deltaTime);
frontTire.AddTorque(-movement * speed * UnityEngine.Time.deltaTime);
carRigidBody.AddTorque(-movement * carTorque * UnityEngine.Time.deltaTime);
}
gas = gas - gasConsumption * UnityEngine.Time.deltaTime * Mathf.Abs(movement);
}
private void OnTriggerEnter2D(Collider2D collision)
{
collidedCoinValue = collision.gameObject.GetComponent<Coin>().coinValue;
moneyAmount += collidedCoinValue;
Destroy(collision.gameObject);
}
}