- Home /
When 2D Car Turns Upside Down, Game Over Scene Should appear.
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);
}
}
Answer by Ermiq · Feb 26, 2021 at 07:16 AM
For empty fuel:
void Update()
{
if (gas <= 0)
SceneManager.LoadScene("GameOverScene");
}
For the car upside down: basically need to just check if the car's up vector points down. But if the car might be upside down while in the air and it shouldn't end the game unless the car stopped spinning and actually stopped on the ground upside down, then you also need to check the car's rigidbody velocity, if the velocity is close to 0 it means the car doesn't move, so if the car is upside down and the car's rigidbody velocity is close to 0 -> game over
. But you don't move the car with rigidBody.AddForce()
, so that might not work. However, just in case, I would do it like this:
if (Vector3.sqrMagnitude(carRigidBody.velocity) < 0.1f)
// car seem to not moving
To check if the car is upside down: you can use Vector2.Dot()
to pass the car's transform.up
vector and the world Vector2.up
vector. If the vectors are pointing in the complete opposite directions then the Vector2.Dot(transform.up, Vector2.up)
will return -1. If they are perpendicular, it will return 0. So, let's check if the Dot product is somewhere between -0.5 an -1 then:
float dot = Vector2.Dot(car.transform.up, Vector2.up);
if (dot <= -0.5f) // the car is upside down
SceneManager.LoadScene("GameOverScene");
So, the overall code would look like:
void Update()
{
if (gas <= 0)
SceneManager.LoadScene("GameOverScene");
float dot = Vector2.Dot(car.transform.up, Vector2.up);
if (dot <= -0.5f) // the car is upside down
if (Vector3.sqrMagnitude(carRigidBody.velocity) < 0.1f)
SceneManager.LoadScene("GameOverScene");
}
Thanks bro for the detailed explanation and reply. It really helped me a lot THANKS AGAIN! @Ermiq
error CS0426: The type name 'sqrMagnitude' does not exist in the type 'Vector3' --- Need Help is your Script @Ermiq
It's Vector3.SqrMagnitude
with a capital 'S'. @MTSKrishna
Ah, yeah. It should be like this: carRigidBody.velocity.sqrMagnitude
.
error CS1955: Non-invocable member 'Vector2.sqrMagnitude' cannot be used like a method ------ need help again! @Ermiq