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...");
}
}
}