- Home /
How make breaks and maximum speed
Hi, I'm trying to make a clone of Hill climb racing. But I don’t know how to make brakes for the car and maximum speed limit, can you suggest me how i can do it? PLEASE HELP ME Thanks in advance
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public Rigidbody2D carRigidbody;
public Rigidbody2D backTire;
public Rigidbody2D frontTire;
public float speed = 20;
public float carTorque = 10;
private float movement;
public float maxSpeed = 100f;
public bool grounded = false;
public LayerMask map;
public Transform ring;
public Text coinsText;
private int coinsInt = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
coinsText.text = coinsInt.ToString();
if (grounded == true)
{
carTorque = 0;
}
else if (grounded == false)
{
carTorque = 100;
}
movement = Input.GetAxis("Horizontal");
grounded = Physics2D.OverlapCircle(ring.transform.position, 0.25f, map);
}
private void FixedUpdate()
{
if (GetComponent<Rigidbody>().velocity.magnitude > maxSpeed)
{
GetComponent<Rigidbody>().velocity = GetComponent<Rigidbody>().velocity.normalized * maxSpeed;
}
backTire.AddTorque(-movement * speed * Time.fixedDeltaTime);
frontTire.AddTorque(-movement * speed * Time.fixedDeltaTime);
carRigidbody.AddTorque(-movement * carTorque * Time.fixedDeltaTime);
}
void OnTriggerEnter2D(Collider2D coins)
{
coinsInt++;
Destroy(coins.gameObject);
}
}
Comment