can't get Raycast to work properly

Hi,
I was trying to make a simple FPS gun. But I can’t get the code to work. The code is attached to a FPS camera and has a raycast collider. This is the code with debugging code I was using the last time it allways returns hit nothing… Why? what am I doing wrong here?

#pragma strict
var myObject : GameObject;

function Update()
{
	Debug.DrawRay (transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.green);
	if (Input.GetButtonDown("Fire1")) 
	{
		var ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));
		var hit : RaycastHit;
		ray.origin = transform.position;
		ray.direction = transform.TransformDirection(Vector3.forward);
		
		Debug.Log("pressed fire1");
		
		if (collider.Raycast (ray, hit, 1000.0)) 
		{
			Debug.DrawLine (ray.origin, hit.point, Color.red);
			Debug.Log(hit.point);
			myObject.transform.position = hit.point;
		}
		else
		{
			Debug.Log("Hit nothing...");
		}
			
	}
}

collider.Raycast() only returns true if the cast Ray intersects with that specific “collider”. I believe your specific code tests to see whether the player shot himself :smile:

Perhaps you want Physics.Raycast(), which casts a Ray against all colliders in the scene?

Thank you very much it works like a charm now :smile: