Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MTSKrishna · Feb 25, 2021 at 04:57 PM · 2d2d game2d-physics2d-gameplaycar game

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);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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");
 }
Comment
Add comment · Show 16 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AbandonedCrypt · Feb 26, 2021 at 08:12 AM 0
Share

Velocity updates when adding force.

avatar image MTSKrishna · Feb 27, 2021 at 09:28 AM 0
Share

Thanks bro for the detailed explanation and reply. It really helped me a lot THANKS AGAIN! @Ermiq

avatar image MTSKrishna · Feb 27, 2021 at 11:57 AM 0
Share

error CS0426: The type name 'sqrMagnitude' does not exist in the type 'Vector3' --- Need Help is your Script @Ermiq

avatar image Llama_w_2Ls MTSKrishna · Feb 27, 2021 at 12:09 PM -2
Share

It's Vector3.SqrMagnitude with a capital 'S'. @MTSKrishna

avatar image Ermiq MTSKrishna · Feb 27, 2021 at 12:19 PM 0
Share

Ah, yeah. It should be like this: carRigidBody.velocity.sqrMagnitude.

avatar image MTSKrishna Ermiq · Feb 27, 2021 at 12:37 PM 0
Share

error CS1955: Non-invocable member 'Vector2.sqrMagnitude' cannot be used like a method ------ need help again! @Ermiq

Show more comments
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

288 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to make a 2d character three-dimensional? 1 Answer

Stuttering in simple 2D game using interpolation? 1 Answer

Why Is there A slight jitteryness on player movement and camera? 0 Answers

Why didnt my Collider works? 0 Answers

Death Counter dont work when changing scenes 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges