hillclimb2d/Assets/Scripts/CarController.cs
2025-03-18 20:37:04 +05:30

58 lines
1.5 KiB
C#

using UnityEditor.SearchService;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CarController : MonoBehaviour
{
public float fuel = 1;
public float fuelconsumption = 0.1f;
public float fuelconsumptionSpeed = 0.1f;
public Rigidbody2D carRigidbody;
public Rigidbody2D BackTire;
public Rigidbody2D FrontTire;
public float speed = 20;
private float movement;
public float CarTorque = 10;
public UnityEngine.UI.Image image;
public float curSpeed;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
movement = Input.GetAxis("Horizontal");
image.fillAmount = fuel;
curSpeed = Mathf.Abs(FrontTire.angularVelocity);
}
public GameObject gameoverpanel;
private void FixedUpdate()
{
if(fuel>0)
{
if(curSpeed < 1000){
BackTire . AddTorque(-movement* speed * Time.fixedDeltaTime);
FrontTire . AddTorque(-movement* speed * Time.fixedDeltaTime);
}
carRigidbody.AddTorque(-movement * CarTorque * Time.fixedDeltaTime);
}else
{
gameoverpanel.SetActive(true);
}
fuel -= fuelconsumption = Mathf.Abs(movement * fuelconsumptionSpeed * Time.deltaTime);
}
public void Restart()
{
SceneManager.LoadScene(0);
}
}