Search Unity

Problem in designing collision response of 2D GUI images in unity.

Discussion in 'Scripting' started by Atul Prakash, Sep 17, 2012.

  1. Atul Prakash

    Atul Prakash

    Joined:
    Mar 20, 2012
    Posts:
    31
    Dear Folks,

    Currently, I am working on a game where I need to design collision system for 2D GUI images in unity, just like below:


    Actually I have implemented the collision detection code myself taking just 2 objects (e.g. DICE1 DICE2) but, I am getting problem in designing the collision response code. What I am doing, is very simple and my code looks like:

    Code (csharp):
    1.  
    2. //DICE 1 MOVEMENT
    3. if(!dice1_right_move_flag){
    4.     dice1_x += dice1_speed * Time.deltaTime;
    5. }else{
    6.     dice1_x -= dice1_speed * Time.deltaTime;
    7. }
    8.  
    9. if(!dice1_up_move_flag){
    10.     dice1_y += dice1_speed * Time.deltaTime;
    11. }else{
    12.     dice1_y -= dice1_speed * Time.deltaTime;
    13. }
    14.        
    15.         if(dice1_x < 0){
    16.             dice1_right_move_flag = false;
    17.         } else if((dice1_x + dice1_image.width)> Screen.width){
    18.             dice1_right_move_flag = true;
    19.         }
    20.        
    21.         if(dice1_y < 0){
    22.             dice1_up_move_flag = false;
    23.         } else if((dice1_y + dice1_image.height)> Screen.height){
    24.             dice1_up_move_flag = true;
    25.         }
    26.  
    27.  
    28. //DICE 2 MOVEMENT
    29. if(!dice2_right_move_flag){
    30.     dice2_x += dice2_speed * Time.deltaTime;
    31. }else{
    32.     dice2_x -= dice2_speed * Time.deltaTime;
    33. }
    34.  
    35. if(!dice2_up_move_flag){
    36.     dice2_y += dice2_speed * Time.deltaTime;
    37. }else{
    38.     dice2_y -= dice2_speed * Time.deltaTime;
    39. }
    40.        
    41.         if(dice2_x < 0){
    42.             dice2_right_move_flag = false;
    43.         } else if((dice2_x + dice2_image.width)> Screen.width){
    44.             dice2_right_move_flag = true;
    45.         }
    46.        
    47.         if(dice2_y < 0){
    48.             dice2_up_move_flag = false;
    49.         } else if((dice2_y + dice2_image.height)> Screen.height){
    50.             dice2_up_move_flag = true;
    51.         }
    52.  
    53. //COLLISION OF DICE 1 WITH DICE 2
    54.  
    55. // 1
    56. if ((dice1_x >= dice2_x)  (dice1_x < dice2_x + dice2_image.width)  (dice1_y >= dice2_y)  (dice1_y < dice2_y + dice2_image.height)) {
    57.             dice1_up_move_flag = true;
    58.             dice1_right_move_flag = true;
    59.            
    60.             dice2_up_move_flag = false;
    61.             dice2_right_move_flag = false;
    62.             print ("1");
    63. // 2
    64. } else if ((dice1_x + dice1_image.width >= dice2_x) (dice1_x+ dice1_image.width < dice2_x+ dice2_image.width)  (dice1_y >= dice2_y)  (dice1_y < dice2_y + dice2_image.height)) {
    65.             dice1_up_move_flag = true;
    66.             dice1_right_move_flag = true;
    67.            
    68.             dice2_up_move_flag = false;
    69.             dice2_right_move_flag = false;
    70.             print ("2");
    71. // 3
    72. } else if ((dice1_x >= dice2_x) (dice1_x < dice2_x+ dice2_image.width) (dice1_y+ dice1_image.height >= dice2_y) (dice1_y+ dice1_image.height < dice2_y+ dice2_image.height)) {
    73.             dice1_up_move_flag = true;
    74.             dice1_right_move_flag = true;
    75.            
    76.             dice2_up_move_flag = false;
    77.             dice2_right_move_flag = false;
    78.             print ("3");
    79. // 4
    80. } else if ((dice1_x + dice1_image.width >= dice2_x)  (dice1_x + dice1_image.width < dice2_x + dice2_image.width)  (dice1_y +  dice1_image.height >= dice2_y)  (dice1_y + dice1_image.height < dice2_y + dice2_image.height)) {
    81.             dice1_up_move_flag = true;
    82.             dice1_right_move_flag = true;
    83.            
    84.             dice2_up_move_flag = false;
    85.             dice2_right_move_flag = false;
    86.             print ("4");
    87. // 5
    88. } else {
    89.  
    90. }
    91.  
    92.  
    I am doubtful regarding my collision response conditions, basically the last 2 conditions of collision code. It would be great if you people help me in figuring out the problem. Please help.

    Looking forward for an early reply.

    Regards,
    Atul Prakash Singh