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. Dismiss Notice

Problem with using Raycast

Discussion in 'Scripting' started by Hossein361224, Aug 2, 2014.

  1. Hossein361224

    Hossein361224

    Joined:
    Aug 16, 2013
    Posts:
    8
    First sorry for bad English
    I just trying to use this code to start using raycast :
    Code (JavaScript):
    1. function Update () {
    2.  
    3.       if (Physics.Raycast(transform.position,transform.forward ,5))
    4.       {
    5.             Debug.Log('yes');
    6.        }
    7.       else Debug.Log('No');
    8.  
    9. }
    I assign this code to first person controller and when distance to an object is less than 5 its message changes to "Yes" but after that message not change and it will be "yes" forever.
    Please tell me where is my problem.
    I use Unity 3D 4.5.2f1
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    First, please show the actual code you are using. As far as I can see, this one is not compilable. And a screenshot with the situation would help too.

    Do you have any error messages in the console?
     
  3. Hossein361224

    Hossein361224

    Joined:
    Aug 16, 2013
    Posts:
    8
    I assign this code to the "First person controller" :

    when i start the game and my distance is greater than 5 , console write "No" :

    when my distance is less than 5 , console write "Yes" :

    But after that even when player are so far , console write "Yes" :

    I have a same problem when using HitInfo. just console write name of first two objects , after that just console write name of second object.
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It works like a charm here. Did you try to deactivate the collider of your cube gun? The raycast may hit that one.
     
  5. Hossein361224

    Hossein361224

    Joined:
    Aug 16, 2013
    Posts:
    8
    Yes , gun has no collider. I'm really confused about that.
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Try that:
    Code (csharp):
    1. #pragma strict
    2.  
    3. var showGizmos : boolean;
    4. var hitPosition : Vector3;
    5.  
    6. function Update () {
    7.    var hit : RaycastHit;
    8.    if (Physics.Raycast (transform.position, transform.forward, hit, 5))
    9.    {
    10.      showGizmos = true;
    11.      hitPosition = hit.point;
    12.      Debug.Log("Yes");
    13.    } else {
    14.      showGizmos = false;
    15.      Debug.Log("No");
    16.    }
    17. }
    18.  
    19. function OnDrawGizmos () {
    20.    if (showGizmos) {
    21.      Gizmos.color = Color.green;
    22.      Gizmos.DrawSphere (hitPosition, 0.2);
    23.    }
    24. }
    Make sure that Gizmos are drawn in the game view. Like that you are going to see the actual hit point.
     
  7. Hossein361224

    Hossein361224

    Joined:
    Aug 16, 2013
    Posts:
    8
    I really Appreciate for your attention. i still have that problem. does this code work correctly for you?



     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Yes it works. Even better if you activate Gizmos at the top right of the game view :)
     
  9. Hossein361224

    Hossein361224

    Joined:
    Aug 16, 2013
    Posts:
    8
    I test your code in new scene and still same problem. this is really strange problem.

     
  10. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You can use Debug.Log to show the hitPosition and the transform.position. Like that you can find out what is going on.

    Where did you add the raycast script?
     
  11. Hossein361224

    Hossein361224

    Joined:
    Aug 16, 2013
    Posts:
    8
    I drag and drop raycast script on "First person controller".
    I change Debog.Log("Yes") to this :
    Code (JavaScript):
    1. Debug.Log("Yes" + hitPosition);
    And at start console write "No" , then when player come near object , console write the hit position and then when i am away from the object , console still write the same position.
    And note that position of two different place on object according to console are same!!!





     
  12. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Do you get any error messages? Are you sure you only placed the scripts once?

    Please show a screenshot of the consoler.
     
  13. Hossein361224

    Hossein361224

    Joined:
    Aug 16, 2013
    Posts:
    8
    I placed the script once and i get no error. I found that when there is no object in front of player , console stop sending message. it just can't write "No". It seem "If" condition is always true and "else" Commands can't run.
    Console screen shot :
     
  14. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Deselect "Collapse" in the Console.
     
    Hossein361224 likes this.
  15. Hossein361224

    Hossein361224

    Joined:
    Aug 16, 2013
    Posts:
    8
    I'm really dumb. :( Thanks for your help. believe it or not i spend 3 day to solve this problem. thank you.:)
     
  16. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    The good thing is that you have a really compact console view ;)
     
    Hossein361224 likes this.