So, I’ve come up with a script that tells me what I’m looking at and then makes me move toward it when I hold the “Fire1” button. However when I press this button it gives me a NullReferenceException. I can’t seem to locate the problem.
The script still works but I would like “not” to get the error report.
I have tried to locate the problem but my brain is failing me it seems.
And help would be appreciated!
private var distance : float = 20.0;
var speed : float;
var moveToMe : Transform;
function Update () {
var cam : Transform = Camera.main.transform;
var ray = new Ray(cam.position, cam.forward);
var hit : RaycastHit;
if(Physics.Raycast(ray, hit, distance)){
var moveToMe = hit.transform;
Debug.Log(hit.transform.name);
}
if (Input.GetButton("Fire1") && hit.collider.tag == "Lerpz"){
transform.position = Vector3.Lerp(transform.position, moveToMe.position, Time.deltaTime * speed);
}
Debug.DrawLine(ray.origin, ray.origin + ray.direction * distance);
}