Raycast not working as expected

OK, I have a sphere(with a spherecollider and a rigidbody(not gravity) and a camera) and this sphere has the MouseLook(with x and y axes) then I make a simple movement for the sphere(I am doing something like forge in halo) and I add a script that makes a raycast forward of the player but this doesn’t work as I want because when I rotate my player the raycast is always the same not matters how I do it, I have try many thinks like:

if(Physics.Raycast(transform.position,transform.forward,hit,10.0)){
if(Input.GetKeyDown(KeyCode.Space)){
hit.collider.transform.tag = "selected";
}
}

if(Physics.Raycast(transform.position,Vector3.forward,hit,10.0)){
if(Input.GetKeyDown(KeyCode.Space)){
hit.collider.transform.tag = "selected";
}
}

var fwd = transform.TransformDirection (Vector3.forward);
if(Physics.Raycast(transform.position,fwd,hit,10.0)){
if(Input.GetKeyDown(KeyCode.Space)){
hit.collider.transform.tag = "selected";
}
}

And anyone of this seems to work, please help me I don’t know what is wrong

Is MouseLook just turning your camera and not your sphere? I don’t see enough code to know if that is the case or not, but if the sphere IS being turned then the first of the 3 chunks of code you posted should be correct.

The second one, using Vector3.forward, always casts the ray “North” of you. The third, using fwd = transform.TransformDirection (Vector3.forward); should cast the ray in the direction you are facing, But transform.forward, which you have in the 1st one, is a shortcut for that.

A trick for testing is to draw the ray:

Debug.DrawRay(transform.position, fwd*10.0, color.Red);

You will only be able to see it in scene mode, but it still gives a clue if the ray is the wrong way, too short… . Also take a look at testing with Debug.Log() when you get a hit (maybe it is hitting, but setting the tag is messing up.)