Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 · Mar 19, 2021 at 03:23 PM · 2dcollision2d gamecar2d collision

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!

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

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!

Comment
Add comment · Show 7 · 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 MTSKrishna · Mar 20, 2021 at 11:26 AM 0
Share

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

avatar image jackmw94 MTSKrishna · Mar 20, 2021 at 11:43 AM 0
Share

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.

avatar image MTSKrishna jackmw94 · Mar 20, 2021 at 03:47 PM 0
Share

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

@jackmw94

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

330 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

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


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