Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 yiddo1993 · Jun 08, 2019 at 06:10 PM · scripting problemcollisioncollision detectioncollision2d

Having problems with obstacle collision killing the player

I know on the surface this is a ridiculously nooby question which has plenty of tutorials online for it, however I have tried following all of them and I cannot for the life of me get it to work. I'm working on a small endless runner for Android and I have multiple obstacles in the game, all using the tag Obstacle. I have a player with the Player tag also. I want the player to immediately die when coming into contact with an obstacle. Easy right?

However, none of the methods I've tried to implement have been successful. When I tried to implement it with health and damage it never registered it in the debug log, yet it must have identified the tag as it destroyed the game object. I've now followed another tutorial where it links together with bringing up a game over screen and having the option to restart, and in this one it was advised to not destroy the object but to use SetActive(false). Now the player just goes through the obstacles and nothing happens. My obstacle collisions are triggers and their script only contains the functionality for them to move across the screen. Nothing else.

Here is the entirety of my Player script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Events;
 
 public class Player : MonoBehaviour
 {
     public Animator animator;
     public UnityEvent OnLandEvent;
 
     public float Jumpforce;
     private float moveinput;
     private Rigidbody2D Myrb2d;
 
     private bool isGrounded;
     public Transform groundcheck;
     public float checkRadius;
     public LayerMask whatIsGround;
 
     private int extraJumps;
     public int extraJumpsValue;
 
     public GameObject gameOverText, restartButton;
 
     // Start is called before the first frame update
     void Start()
     {
         gameOverText.SetActive(false);
         restartButton.SetActive(false);
 
         extraJumps = extraJumpsValue;
         Myrb2d = gameObject.GetComponent<Rigidbody2D>();
         animator = gameObject.GetComponent<Animator>();
         
         if (OnLandEvent == null)
             OnLandEvent = new UnityEvent();
     }
 
     // Update is called once per frame
     void FixedUpdate(){
 
         isGrounded = Physics2D.OverlapCircle(groundcheck.position, checkRadius, whatIsGround);   
         
     }
     private void Update(){
 
         if(isGrounded == true){
             extraJumps = extraJumpsValue;
             OnLandEvent.Invoke();
         }
 
         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began && extraJumps > 0) {
             Myrb2d.velocity = Vector2.up * Jumpforce;
             extraJumps--;
             isGrounded = false;
             animator.SetBool("IsJumping", true);
 
         } else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began && extraJumps == 0 && isGrounded == true) {
             Myrb2d.velocity = Vector2.up * Jumpforce;
         }            
     
     }
 
     void OnCollisionEnter2D (Collision2D other)
     {
         if (other.gameObject.tag.Equals("Obstacle"))
         {
             gameOverText.SetActive(true);
             restartButton.SetActive(true);
             gameObject.SetActive(false);
         }
     }
 
     public void OnLanding()
     {
         animator.SetBool("IsJumping", false);
     }
 }
 
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 BradyIrv · Jun 08, 2019 at 07:18 PM

If your obstacle colliders are set to Triggers, then you have to use this method:

 private void OnTriggerEnter2D(Collider2D other) {
     // Also instead of tag.Equals(), you can just say:
     if (other.transform.tag == "Obstacle") {
          // Do something
     }
 }
 
 // As opposed to using the method OnCollisionEnter2D


Comment
Add comment · Show 1 · 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 yiddo1993 · Jun 08, 2019 at 10:48 PM 0
Share

This solved it. Thanks a lot! :)

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

236 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

Related Questions

object isn't detecting collisions 1 Answer

Sprite ignoring collison 1 Answer

How to set collision for an object with specific collider size? 3 Answers

if statement not working when detecting collision between two prefabs 1 Answer

Disable collision before collision actually happens 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