Search Unity

How can I see if my character has walls to the left or right of it?

Discussion in 'Scripting' started by Zwiebel, May 25, 2015.

  1. Zwiebel

    Zwiebel

    Joined:
    Jul 23, 2013
    Posts:
    56
    Hi!

    I would like to see with raycasting, if my characters have walls left, right or forward of them, I use this code for this:
    Code (csharp):
    1.  
    2. if (Physics.Raycast(transform.position, fwd, out objectHit, 1.2f)) {
    3.             if (objectHit.collider.tag == "Wall") {
    4.                 fwdFree = false;
    5.                 Debug.Log("FWDDDDDDDD");
    6.             }
    7.             else {
    8.                 fwdFree = true;
    9.             }
    10.         }
    11.         else {
    12.                 fwdFree = true;
    13.         }
    14.    
    15.  
    16.         if (Physics.Raycast(transform.position, rght, out objectHit, 1.2f)) {
    17.             if (objectHit.collider.tag == "Wall") {
    18.                 Debug.Log("RIGHTTTTTTTT");
    19.                 rghtFree = false;
    20.             }
    21.             else {
    22.                 rghtFree = true;
    23.             }
    24.         }
    25.         else {
    26.             rghtFree = true;
    27.         }
    28.  
    29.         if (Physics.Raycast(transform.position, lft, out objectHit, 1.2f)) {
    30.             if (objectHit.collider.tag == "Wall") {
    31.                 Debug.Log("LEFTTTTTTT");
    32.                 lftFree = false;
    33.             }
    34.             else {
    35.                 lftFree = true;
    36.             }
    37.         }
    38.         else {
    39.             lftFree = true;
    40.         }
    41.  
    42.  
    However, while my FWD direction works great, my the RGHT and LFT directions get Debugged always, and not just when there is a wall near the character.
    I would like to ask you, what is the reason because of it, and how could I solve it?

    Thanks in advance!
     
  2. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Would you mind posting the rest of your Raycast code so that we can see how you're defining FWD, RGHT and LFT?
     
  3. Zwiebel

    Zwiebel

    Joined:
    Jul 23, 2013
    Posts:
    56
    Yes, sorry, I have added it first, but deleted it for some reason :)
    I could have make it work with the following code, but for some reason the left debug give me LEFTTTTTT, even if there isn't wall near the character.

    Code (csharp):
    1.  
    2.  
    3. void Update () {
    4.         elapsedTime += Time.deltaTime;
    5.    
    6.         if (elapsedTime >= movingTime) {
    7.             bool moved = false;
    8.             if (!moved) {
    9.  
    10.                 fwd = transform.forward;
    11.                 rght = transform.right;
    12.                 lft = Vector3.left;
    13.            
    14.  
    Then I use this code to determine the fwdFree, lftFree, rghtFree variables' value: (I call it in update but just if elapsedTime >= movingTime)

    Code (csharp):
    1.  
    2. if (Physics.Raycast(transform.position, fwd, out objectHit, rayDistance)) {
    3.                     if (objectHit.collider.tag == "Wall") {
    4.                         fwdFree = false;
    5.                         Debug.Log("FWDDDDDDDD");
    6.                     }
    7.                     else {
    8.                         fwdFree = true;
    9.                     }
    10.                 }
    11.                 else {
    12.                         fwdFree = true;
    13.                 }
    14.            
    15.  
    16.                 if (Physics.Raycast(transform.position, rght, out objectHit, rayDistance)) {
    17.                     if (objectHit.collider.tag == "Wall") {
    18.                         Debug.Log("RIGHTTTTTTTT");
    19.                         rghtFree = false;
    20.                     }
    21.                     else {
    22.                         rghtFree = true;
    23.                     }
    24.                 }
    25.                 else {
    26.                     rghtFree = true;
    27.                 }
    28.  
    29.                 if (Physics.Raycast(transform.position, lft, out objectHit, rayDistance)) {
    30.                     if (objectHit.collider.tag == "Wall") {
    31.                         Debug.Log("LEFTTTTTTT");
    32.                         lftFree = false;
    33.                     }
    34.                     else {
    35.                         lftFree = true;
    36.                     }
    37.                 }
    38.                 else {
    39.                     lftFree = true;
    40.                 }
    41.  
     
  4. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    You might also want to use
    Code (csharp):
    1. Debug.DrawLine
    or
    Code (csharp):
    1. Debug.DrawRay
    in your Update function to see in the editor where your ray are landings.

    It helps a ton when working with raycast.
     
    krougeau likes this.
  5. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    is there a reason why your lft is defined as Vector3.left, and not by the same method as right and forward? Shouldn't it be transform.left? It may be transform.left doesn't exist, but then you can just inverse your right (-transform.right). Don't remember...
     
    krougeau likes this.