Hey guys,
I am currently trying to use a Raycast in Order to trigger an interaction with an Object in my Gameworld, in this Case an Item. For this I am using this Skript:
public class PlayerController : MonoBehaviour
{
private Vector3 fwd;
private RaycastHit hit;
public float range;
public Camera mainCamera;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown("e"))
{
fwd = mainCamera.transform.forward;
if (Physics.Raycast(mainCamera.transform.position, fwd, out hit, range))
{
if (hit.collider.gameObject.tag == "Interactable")
{
Interactable i = hit.collider.gameObject.GetComponent<Interactable>();
i.Interact();
}
}
}
}
}
For now, the only thing that should happen if the Raycast hits an Item is to show the message “Interacted” in the Console.
Now if i stand in front of my Object nothing will happen:
But if I use my crouch function, which will move my Player down a bit, it works just fine:
I added the Skript to my Player Gameobject and connected the Main Camera, I also tried to higher the radius but it didn’t help.
What am I doing wrong?