RayCastHit

Hello,

I have 1 problem with raycast.

Code:

#pragma strict

var dot : Transform;
var defDotPos : Vector3 = Vector3(0, 1000, 0);

function Update()
{
    var ray = transform.TransformDirection(Vector3.forward);
    var hit : RaycastHit;

    if (Physics.Raycast(transform.position , ray , hit, 150))
    {
        dot.position = hit.point;
    }
    else 
    {
        dot.position = defDotPos;
    }
}

This script is supposed to show a point(sphere) on the object which is in front of the character, but actually it loops sphere between character and that object. here is picture so you can see Screenshot by Lightshot

Its sidescroller im talking about.

Really hard to tell what exactly you’re trying to do the code is a bit vague. But my best guess is you have some kind of collider intercepting your ray cast before it’s expected collision. Most likely the character object if the ray is being cast from inside the character.

Set a layer for your character and ignore that layer in the raycast.

this dude is badass: (fix for problem)