Im using Raycasting to decide if an object is clickable through tags on objects with colliders, but when Im looking at objects at certain angles, the standard first person controller gets in the way, basically effecting line of sight from itself. I have the Raycast originating from the camera child of the controller because I have the mouse locked to centre screen and the GUI crosshair showing where the mouse is (also changing colour when a “clickable” object is under the crosshair) so the Raycast is pointing in exactly the same direction as the player is looking. Simply moving the camera forward results in the camera ending up behind objects (including walls) when you get too close.
So what I need is a way for the Raycast to start operating in front of where the point of origin of the raycast is, Im afraid if I just move the raycast forward it would not allign to the Crosshair anymore. This is the code Im using.
using UnityEngine;
using System.Collections;
public class Crosshair : MonoBehaviour
{
private Color curcol;
public float interactionDistance = 3f;
public bool imnear;
void Start ()
{
Screen.lockCursor = true;
}
void OnGUI()
{
GUI.color = curcol;
GUI.Label (new Rect(Screen.width/2,Screen.height/2, 50, 50), "+");
}
void Update()
{
RaycastHit hit;
Debug.DrawRay(transform.position, transform.forward * interactionDistance);
if(Physics.Raycast (transform.position, transform.forward, out hit, interactionDistance))
{
if(hit.collider.tag == "Clickable")
{
imnear = true;
}
else
{
imnear = false;
}
}
else
{
imnear =false;
}
if(imnear == true)
{
curcol = Color.green;
}
else
{
curcol = Color.white;
}
}
}