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 matthew_gigante · Dec 03, 2017 at 09:28 PM · 2dcollider2doncollisionenteroncollisionstayoncollisionexit

OnCollisionStay2D not working in the way I would like.

I am making a game about a miner who destroys blocks to fall down. There are 6 blocks per floor, and I want the miner to be able to destroy the block he is standing on. The issue is that he will only destroy a block if the down arrow is tapped AND he is moving. I would like the miner to be able to break blocks even if he is standing still and colliding with a block. My code is here:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerController : MonoBehaviour {
 
     //this public variable will contain the player GameObject
     public GameObject player;
     //this will be the speed of our player, which can be changed in the Unity inspector
     public float speed;
 
 
 
     void Update ()
     {
         //this if/if else statement here will check to see if the player would go off the right or left of the screen
         //if the player is about to go off the screen, instead put them back where in the x position they were at before that
         if (player.transform.position.x <= -2.6f) {
             player.transform.position = new Vector3 (-2.6f, transform.position.y, 0);
         } else if (transform.position.x >= 2.6f) {
             player.transform.position = new Vector3 (2.6f, transform.position.y, 0);
         }
         //These two if statements wait for input, and will move the player left or right if the arrows are pressed
         //this is placeholder code, eventually this will be replaced with onMouseDown and buttons so that it may be optimized for android use
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             transform.position += Vector3.left * speed * Time.deltaTime;
         }
         if (Input.GetKey(KeyCode.RightArrow))
         {
             transform.position += Vector3.right * speed * Time.deltaTime;
         }
     }
     //here, the player will detect collisions with other objects
     //the purpose of having these collisions will be to decide how the player
     //can react to the object
     //ideally, there will be different types of terrain the player can stand on
     //each terrain will take a different amount of taps to destroy, then they may fall down
     //into the next floor of terrain
     void OnCollisionStay2D(Collision2D collide){
         if (Input.GetKeyDown (KeyCode.DownArrow)) {
             Destroy (collide.gameObject);
         }
         Debug.Log (collide.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 matthew_gigante · Dec 08, 2017 at 05:22 PM

I ended up casting a ray right under the player object to determine which objects the ray collides with, the ray does not detect or interact with the spawnZone trigger, which is what I need to not be destroyed so that when the player hits it, the next wave of blocks spawns:

 //Matthew Gigante 2017
 //A helpful tutorial related to this code:
 //https://unity3d.com/learn/tutorials/projects/2d-ufo-tutorial/controlling-player
 
 //the current objective of this script is to move the player
 //and to prevent them from falling off the sides of the screen.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerController : MonoBehaviour {
 
     //this public variable will contain the player GameObject
     public GameObject player;
     //this will be the speed of our player, which can be changed in the Unity inspector
     public float speed;
     //allows access to the floorSpawner script, which will help us instantiate new floor prefabs
     public floorSpawner other;
     public blockScript scriptForBlocks;
 
     void Update ()
     {
         //this if/if else statement here will check to see if the player would go off the right or left of the screen
         //if the player is about to go off the screen, instead put them back where in the x position they were at before that
         if (player.transform.position.x <= -2.6f) {
             player.transform.position = new Vector3 (-2.6f, transform.position.y, 0);
         } else if (transform.position.x >= 2.6f) {
             player.transform.position = new Vector3 (2.6f, transform.position.y, 0);
         }
         //These two if statements wait for input, and will move the player left or right if the arrows are pressed
         //this is placeholder code, eventually this will be replaced with onMouseDown and buttons so that it may be optimized for android use
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             transform.position += Vector3.left * speed * Time.deltaTime;
         }
         if (Input.GetKey(KeyCode.RightArrow))
         {
             transform.position += Vector3.right * speed * Time.deltaTime;
         }
         if(Input.GetKeyDown(KeyCode.DownArrow))
         {
             //middle vector2 is size (Vector2.one)
             //creates a collider for the ground, detects overlap between player and whatever ground object is below it
             //if there is an object directly under the player object, destroy it
             Collider2D ground = Physics2D.OverlapBox (transform.position + Vector3.up * -0.6f, Vector2.one * .01f, 0f);
             print (ground.gameObject);
 
             if(ground.gameObject.name != "spawnZone(Clone)"){
             Destroy (ground.gameObject);
             }
         }
     }
     //runs floorSpawner script's Start function
     void OnTriggerEnter2D(Collider2D coll){
         other.Start ();
     }
 }
Comment
Add comment · 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

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

138 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

Related Questions

Track all colliding Collider2D. 1 Answer

OnCollisionStay2D is ignoring OnCollisionEnter2D 2 Answers

why is there's a gap between colliders2d? 1 Answer

Bullet hit the collider of a child object 1 Answer

Can I make 2d collider at specific angle? 0 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