I am Working on a 3d Game In Unity. So the problem I am facing is with Outlining the object only when player look towards that object, I have a C# Interface "IInteractable", whenever player looks at an object which is IInteractable It Calls function "Looking()" and when player Press "Q" It Calls "Trigger()"
public interface IInteractable
{
void trigger();
void Looking();
void NotLooking();
}
for outlining currently I am doing this "Looking()" calls in each Update function when player look at the object and "trigger()" only calls when player player press "Q" & "NotLooking()" currently have no use
public class exampleObject:MonoBehaviour,IInteractable
{
private Outline outline;
private float timmer=0;
void Start()
{
outline=GetComponent<Outline>();
outline.enabled=false;
}
void Update()
{
if(timmer>5)
{
outline.enabled=true;
timmer-=4;
}else{
outline.enabled=false;
}
}
//Interface functions
public void Looking()
{
timmer+=5;
}
public void NotLooking()
{
outline.enabled=false;
}
public void trigger()
{
// code to interact with the Object
}
}
I Know this is not a good approach and also if I look too long at an Object then after leaving it it still having outline, as we can clearly see the problem in the script;
If Needed Player Script
private Ray ray;
private RaycastHit hit;
[SerializeField] bool alreadyInHand=false;
[SerializeField] IGrabable data;
[SerializeField] float distance=5.0f;
private bool showingText=false;
void Start()
{
}
void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
#region IIntractable
if (Physics.Raycast(ray, out hit, distance))
{
IInteractable interactable=hit.collider.gameObject.GetComponent<IInteractable>();
IGrabable grabObje=hit.collider.gameObject.GetComponent<IGrabable>();
if(interactable!=null)
{
interactable.Looking();
}
if(Input.GetKeyDown(KeyCode.Q) && interactable!=null)
{
interactable.trigger();
}
}
can any one please help me with this, I am new to this field,
I Have tried to make a to check if (peek()==current_Interacatable) then return; else call NotLooking() in that object and remove it from the Queue, But still its not working.