I’m trying to draw an icon on the screen when a person looks at an object they can pick up. The script seems to work fine, but it doesn’t draw the ray when I look at the object when I close to it. I’m trying to take one step at a time, and iron out my problems before go further.
Forgive me for bad code (feel free to help me clean it up if I need to)
using UnityEngine;
using System.Collections;
public class pickup : MonoBehaviour {
public RaycastHit hit;
MeshRenderer obj;
void Start() {
obj = GameObject.FindGameObjectWithTag("myobj").GetComponent(typeof(MeshRenderer))as MeshRenderer;
}
void Update() {
obj.enabled = false;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, out hit, 10))
{
if (hit.collider.gameObject.tag == "object")
{
obj.enabled = true;
Debug.DrawLine(transform.position, hit.point, Color.red);
}
}
}
}
I have a plane attached to the camera with a texture applied to it that sits in front of the camera, disabled. When the person looks at the object, the plane appears, and then disappears if they look away. My object I’m wanting to look at has a tag and a simple box collider, when I look at it, I see the line drawn to the item (in scene view), and the icon appears on the screen. Looks okay. But if I walk too close the item, which is below the camera, I have to look down, and the ray doesn’t hit the object. If I stand far back, it looks like it hits it (except for the very bottom). Why does this happen?