Problem with using Raycast

First sorry for bad English
I just trying to use this code to start using raycast :

function Update () {

      if (Physics.Raycast(transform.position,transform.forward ,5))
      {
            Debug.Log('yes');
       }
      else Debug.Log('No');

}

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

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?

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.

It works like a charm here. Did you try to deactivate the collider of your cube gun? The raycast may hit that one.

Yes , gun has no collider. I’m really confused about that.

Try that:

#pragma strict

var showGizmos : boolean;
var hitPosition : Vector3;

function Update () {
   var hit : RaycastHit;
   if (Physics.Raycast (transform.position, transform.forward, hit, 5))
   {
     showGizmos = true;
     hitPosition = hit.point;
     Debug.Log("Yes");
   } else {
     showGizmos = false;
     Debug.Log("No");
   }
}

function OnDrawGizmos () {
   if (showGizmos) {
     Gizmos.color = Color.green;
     Gizmos.DrawSphere (hitPosition, 0.2);
   }
}

Make sure that Gizmos are drawn in the game view. Like that you are going to see the actual hit point.

I really Appreciate for your attention. i still have that problem. does this code work correctly for you?

Yes it works. Even better if you activate Gizmos at the top right of the game view :slight_smile:

I test your code in new scene and still same problem. this is really strange problem.

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?

I drag and drop raycast script on “First person controller”.
I change Debog.Log(“Yes”) to this :

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!!!

Do you get any error messages? Are you sure you only placed the scripts once?

Please show a screenshot of the consoler.

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 :

Deselect “Collapse” in the Console.

1 Like

I’m really dumb. :frowning: Thanks for your help. believe it or not i spend 3 day to solve this problem. thank you.:slight_smile:

The good thing is that you have a really compact console view :wink:

1 Like