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? 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
Your answer
Follow this Question
Related Questions
How to make physics for rail trolley with gravity changing. Unity 2d game 0 Answers
game is working right in unity but does not work properly on the phone. 0 Answers
bool not working right on collision 0 Answers
Bad android quality ,Bad Android Quality 0 Answers
how to make an background image that has infinite repeating scrolling in horizontal in unity 2d? 0 Answers