- Home /
 
               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
              
 
               
               
               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 ();
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                