Raycast issue. (solved)

I’ve just started working on my idea for a FPS gamepad for multi-touch devices such as the Iphone and Android devices. While no, I don’t have either device, I do intend to get an android device semi-soon. That’s all neither here nor there, just know that I’m working on an FPS control system.

I’m trying to work on my reticle, which I’d like to be able to change the color of the reticle if it’s on something that can collide with it, such as an enemy or an explosive box/barrel. Problem is, it’s not quite working right.

It keeps placing the sphere object that I’m using to debug it’s position directly inside the player camera. It randomly seems to go to the point that it should if I’m jumping around. I thought maybe it was using the sphere or player colliders to activate the raycast hit, but if I add them to a layer mask and add that mask to the ignore when processing the raycast, it just doesn’t work at all. The sphere never moves, and the print only ever shows no collision.

Anyone able to help? Here is my code:

function Update()
{
	//var ray:Ray = new Ray(transform.position, transform.eulerAngles);
	var reticle:GameObject = GameObject.Find("Sphere");
	var ray:Ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2,Screen.height/2));
    var hit:RaycastHit;
	if (Physics.Raycast(ray, hit,  Mathf.Infinity)) 
	{
        Debug.DrawLine (ray.origin, hit.point);
        reticle.transform.position = hit.point;
		print("Hit something");
    }else{
		print("No Collision" + transform.position + transform.eulerAngles);
	}
}

I’ve already searched a bit, messed with it for a few hours, and even looked at the FPS tutorial code and didn’t find anything quite like what I was wanting. I don’t see anything wrong at this point. I’ve used raycasts before when I was using the mouse position to do selection with the RTS thing I’ve been working on (haven’t worked on it in a while) but that is a little different since the camera doesn’t have a character controller.

Debug.Log( "Hit " + hit.collider.name );

You can use this to log what you do hit. Maybe that will help? Your code looks alright to me.

It was because the sphere I was using had a sphere collider and it was colliding with it, so it kept moving the point backwards towards the player because of it.

I thought I had removed the sphere collider, and apparently I didn’t.