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

2D Raycast Collision Detection Issue

Discussion in 'Scripting' started by BudBroesky, Jun 29, 2014.

  1. BudBroesky

    BudBroesky

    Joined:
    Nov 11, 2013
    Posts:
    15
    Hi! I am quite new to C# and have scripted this for a game I'm making (no duh). SO the issue is whenever I test it, the raycasts dont detect collisions. I have the code setup to check whether raycasting is enabled for each direction (up down left right), and if it is and it collides with a layer ("Level" layer), it stops the player. I have a public bool to check whether it is colliding or not, even when the rays are touching the objects with the RIGHT layer (yes they have colliders), nothing happens. Also, my vertical raycasts get drawn in Debug but dont go away (creates trippy abstract art on my screen). As you will see, the vertical raycasts arent programmed to do anything since this is a platformer and vertical raycasts arent needed for basic movement (at least not right now). Here is my code:

    Code (csharp):
    1.  
    2. usingUnityEngine;
    3. usingSystem.Collections;
    4.  
    5. publicclassPlayerMovement : MonoBehaviour {
    6. publicfloatmoveSpeed;
    7.  
    8. publicTransformspawnPoint;
    9.  
    10. publicboolraycastBack;
    11. publicboolbackCollision = false;
    12. publicTransform[] backPoints;
    13.  
    14. publicboolraycastFront;
    15. publicboolfrontCollision = false;
    16. publicTransform[] frontPoints;
    17.  
    18. publicboolraycastUp;
    19. publicboolupCollsion;
    20. publicTransform[] upPoints;
    21.  
    22. publicboolraycastDown;
    23. publicbooldownCollision;
    24. publicTransform[] downPoints;
    25.  
    26. //Usethisforinitialization
    27. voidStart () {
    28.  
    29.  }
    30.  
    31. //Updateiscalledonceperframe
    32. voidUpdate () {
    33. Movement();
    34. CheckCollision();
    35.  }
    36.  
    37. //MovementScript
    38. voidMovement()
    39.  {
    40. if (Input.GetAxisRaw("Horizontal") > 0)
    41.  {
    42. transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
    43. transform.eulerAngles = newVector2(0, 0);
    44. if (raycastFront == true)
    45.  {
    46. RaycastFront();
    47.  }
    48.  }
    49. if (Input.GetAxisRaw("Horizontal") < 0)
    50.  {
    51. transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
    52. transform.eulerAngles = newVector2(0, 180);
    53. if (raycastFront == true)
    54.  {
    55. RaycastFront();
    56.  }
    57.  }
    58. if (Input.GetAxisRaw("Vertical") > 0)
    59.  {
    60. if (raycastUp == true)
    61.  {
    62. RaycastUp();
    63.  }
    64.  }
    65. if (Input.GetAxisRaw("Vertical") < 0)
    66.  {
    67. if (raycastDown == true)
    68.  {
    69. RaycastDown();
    70.  }
    71.  }
    72.  }
    73.  
    74. //RAYCASTINGSTARTSHERE!!!!!!!
    75. voidRaycastBack()
    76.  {
    77. Debug.DrawLine(backPoints[0].position, backPoints[1].position, Color.red);
    78. backCollision = Physics2D.Linecast(backPoints[0].position, backPoints[1].position, 1 << LayerMask.NameToLayer("Level"));
    79.  
    80. Debug.DrawLine(backPoints[2].position, backPoints[3].position, Color.red);
    81. backCollision = Physics2D.Linecast(backPoints[2].position, backPoints[3].position, 1 << LayerMask.NameToLayer("Level"));
    82.  }
    83. voidRaycastFront()
    84.  {
    85. Debug.DrawLine(frontPoints[0].position, frontPoints[1].position, Color.red);
    86. frontCollision = Physics2D.Linecast(frontPoints[0].position, frontPoints[1].position, 1 << LayerMask.NameToLayer("Level"));
    87.  
    88. Debug.DrawLine(frontPoints[2].position, frontPoints[3].position, Color.red);
    89. frontCollision = Physics2D.Linecast(frontPoints[2].position, frontPoints[3].position, 1 << LayerMask.NameToLayer("Level"));
    90.  }
    91. voidRaycastUp()
    92.  {
    93. Debug.DrawLine(upPoints[0].position, upPoints[1].position, Color.red, 1 << LayerMask.NameToLayer("Level"));
    94. upCollsion = Physics2D.Linecast(upPoints[0].position, upPoints[1].position);
    95.  
    96. Debug.DrawLine(upPoints[2].position, upPoints[3].position, Color.red, 1 << LayerMask.NameToLayer("Level"));
    97. upCollsion = Physics2D.Linecast(upPoints[2].position, upPoints[3].position);
    98.  }
    99. voidRaycastDown()
    100.  {
    101. Debug.DrawLine(downPoints[0].position, downPoints[1].position, Color.red, 1 << LayerMask.NameToLayer("Level"));
    102. downCollision = Physics2D.Linecast(downPoints[0].position, downPoints[1].position);
    103.  
    104. Debug.DrawLine(downPoints[2].position, downPoints[3].position, Color.red, 1 << LayerMask.NameToLayer("Level"));
    105. downCollision = Physics2D.Linecast(downPoints[2].position, downPoints[3].position);
    106.  }
    107.  
    108. voidCheckCollision ()
    109.  {
    110. if (backCollision == true)
    111.  {
    112. moveSpeed = 0;
    113.  }
    114. if (frontCollision == true)
    115.  {
    116. moveSpeed = 0;
    117.  }
    118.  }
    119. }
    120.  
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    wow dude this is never gonna work... dunno if something went wrong posting or you actually wrote it like this??

    i.e." publicclassPlayerMovement" should be "public class PlayerMovement"
    or "publicfloatmoveSpeed;" should be "public float moveSpeed;" and so on...
     
  3. BudBroesky

    BudBroesky

    Joined:
    Nov 11, 2013
    Posts:
    15
    haha no im not THAT new! I definitely have my code setup properly. I am getting no errors with my code either! it was definitely an error with unity's forum posting. It looked fine when I posted it but now... yeesh!
     
  4. BudBroesky

    BudBroesky

    Joined:
    Nov 11, 2013
    Posts:
    15
    Just act like all the spaces are in there :)
     
  5. BudBroesky

    BudBroesky

    Joined:
    Nov 11, 2013
    Posts:
    15
    This is what the up and down raycast lines create, dont know if this is the actual raycasts or just the debug.drawlines, but its trippy none the less!
     

    Attached Files:

  6. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    few questions then: i see you use the bools raycastfront ect. but i don't see you changing them true/false anywhere...
    also what are you doing with the points? are they all parented to the player or...?
    there is clearly something wrong here.

    if I were you I'd still go for a Physics2D.Raycast so you can drop the points arrays.
    make a ray with origin your player and the direction you want to cast like Vector2.right and so.
     
  7. BudBroesky

    BudBroesky

    Joined:
    Nov 11, 2013
    Posts:
    15
    I actually got it working, oh, the bool gets changed becasue i am doing "exampleBool = LinecastScript" which controls the true and false of the bool. I rescripted almost the whole thing and now it works prefectly! Lol i guess the issue was I was overloading a single bool and it didnt know what to put to it just stayed blank.
     
  8. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    so the bool that had to initiate it's linecast void was the same that had to be true to cast the line in the first place... doesn't seem such a good plan.
    anyway good thing you rewrote it! I guess you could do something like this in some 10 lines of code...