- Home /
2d car end line error help
hi every one! I am doing a 2d car game in which player (car) should enter the end line. everything is alright but only the end flag. When car collide the 2d flag, Win UI canvas should be set active. I am new to unity and programming field. Bros and Sis pls help! First I try to open new scene. the script that had been attached to End Flag first. the car is not collide with flag, not working fine. I add 2d box collider and triggered it, no use! then I added the script to my car, that also not working. Here is my script : using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class FinishScript : MonoBehaviour { void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.name == "EndFlag") { SceneManager.LoadScene("Finish"); } } }
Then I changed my idea now I need to do is to open a canvas and set it active pls help! I don't know the script for this and where to add the script (which game object) Here is the funny Script : using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class WinUI : MonoBehaviour { public static bool YouWin = false; public GameObject WinMenuUI; void Start() { Time.timeScale = 1f; } void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.name == "EndFlag") { if (YouWin) { Resume(); } else { Win(); } } } public void Resume() { WinMenuUI.SetActive(false); Time.timeScale = 1f; YouWin = false; } void Win() { WinMenuUI.SetActive(true); Time.timeScale = 1f; YouWin = true; } public void LoadMenu() { SceneManager.LoadScene("Main Menu"); Time.timeScale = 1f; } public void Restart() { SceneManager.LoadScene(SceneManager.GetActiveScene().name); Time.timeScale = 1f; } }
some words are just for fun :) PLS PLS HELP!! THANKS IN ADVANCE!
Answer by jackmw94 · Mar 19, 2021 at 05:13 PM
Since your script is checking collisions with the EndFlag object, this script should be on your car and not on the flag since the flag isn't going to collide with itself.
There are two types of collision: real physics collisions where rigidbodies hit each other and bounce off or push each other, these use OnCollisionEnter/Stay/Exit. And secondly, trigger collisions where one collider enters another and they have no physical impact on each other aside from triggering each others' OnTriggerEnter/Stay/Exit functions. You are using the latter but this requires your colliders to have the "isTrigger" flag set.
SO I would have thought that if your finish-checking script is on the car, both the car and end flag objects have colliders and your end flag's collider has "isTrigger" set that then your OnTriggerEnter function will be called when your car gets to the end flag. Let me know if that doesn't work :)
Also side note, sometimes when adding code to your answer it won't format it properly unless it has an empty line before it. You might struggle to get answers with your code all on one line!
Thanks for replay! but That also not working. I add the "FinishScript" to the car, No Impact!! and I use debug.log and the collision is not happening! pls help again @jackmw94
Ah I forgot one step - one of the colliders needs to have a rigid body on it. By default this will turn it into a physics object, if you don’t want this then check “isKinematic” and uncheck “useGravity” to have it behave as it currently does. If I were you I’d apply this to your end flag since rigidbodies expect movement to be handled by physics so using the end flag avoids messing with your current car moving code.
this is a 2d car game and the flag contains 2d box collider so the rigidbody is cannot be added. only rigidbody 2d is accepted and the script contains
OnTriggerEnter2D(Collider2D collision)
so pls help again
Your answer
Follow this Question
Related Questions
Best way to manually calculate collision between GameObjects and a Tilemap without using physics 1 Answer
Unity 2D Collider Safe Sizes 0 Answers
Problem with collision with two colliders simultaneously in Unity 2D 1 Answer
2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers
Why Does Object Keep Getting Faster? 1 Answer