Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Issue with Object Staying Collided

Discussion in 'Scripting' started by oscar_wong67, Aug 26, 2015.

  1. oscar_wong67

    oscar_wong67

    Joined:
    Jul 11, 2015
    Posts:
    2
    Hi everyone,

    My code attempts to detect if the pizza in my game is collided with the user-controlled hand. If it is, then the hand will be able to add velocity to the pizza, effectively tossing it. However, I have colliders on the edge of the screen to prevent the pizza from flying off. When the pizza is collided with the edges, the hand can still add velocity to the pizza even though the hand isn't actually touching it.

    Code (JavaScript):
    1. #pragma strict
    2. var collided = false;
    3. var thrust: float;
    4. var pizza : Rigidbody2D;
    5.  
    6.   function Start() {
    7.       //get the rigidbody of the pizza-small object
    8.    pizza = GameObject.Find("pizza-small").GetComponent.<Rigidbody2D>();
    9.   }
    10. //hand + pizza collision detection
    11. function OnCollisionStay2D(coll: Collision2D) {
    12.      if (coll.gameObject.tag == "Pizza") {
    13.          collided = true;
    14.      }
    15.      else {
    16.          collided = false;
    17.      }
    18. }
    19. function Update() {
    20.      //constantly detects collision
    21.      if (collided === true) {
    22.          Debug.Log("Hand+Pizza");
    23.      }
    24. //Input
    25. if (Input.GetKey(KeyCode.W)) {
    26.      Debug.Log("Up");
    27.      transform.position.y+= 0.2;
    28.      if (collided === true) {
    29.      //Adding force to the pizza
    30.      pizza.velocity = Vector3(0,15,0);
    31.      }
    32. }
    33. //left
    34. else if (Input.GetKey(KeyCode.A) || Input.GetKey("left")) {
    35.      Debug.Log("Left");
    36.      transform.position.x-= 0.1;      
    37. }
    38. //right
    39. else if (Input.GetKey(KeyCode.D) || Input.GetKey("right")) {
    40.      Debug.Log("Right");
    41.      transform.position.x+= 0.1;      
    42. }
    43. //down
    44. else if (Input.GetKey(KeyCode.S) || Input.GetKey("down")) {
    45.      Debug.Log("Down");
    46.      transform.position.y-= 0.1;      
    47. }
    48. //moves the hand back to it's original position - gravity was wonky so I decided against using a rigidbody
    49. else if (transform.position.y > -3.5) {
    50.      transform.position.y-= 0.25;
    51. }
    52. else {
    53.      collided = false;
    54. }
    55. }
    56.  
    The above script handles input and collision. Collided remains true when the pizza is collided with the edge colliders.

    I only want the pizza to gain upward velocity if the hand is colliding with it, and I'm kind of stuck, any insight would be really appreciated!
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    OnCollisionStay is only called in frames when the collision is ongoing. If the collision stops it isn't going to be called.

    If you want to do something when a collision ends (i.e. set your boolean to false) you need to use OnCollisionExit
     
  3. oscar_wong67

    oscar_wong67

    Joined:
    Jul 11, 2015
    Posts:
    2
    I've tried adding OnCollisionExit, but since I can't put it inside of Update(), it appears to not solve anything as it doesn't check collision constantly and I don't know how to implement it.

    Edit: I got it! I was using an if statement to detect the tag earlier, but since I removed that it works!
     
    Last edited: Aug 27, 2015