Hello,
I was making a 2D platformer in which you could press the “retry” button to restart the game. I looked around for a few guides but they all seemed to either be in javascript, or were outdated. I read a specific guide which tells you to use raycasts in order to detect whether or not a gameobject is clicked. However, my code did not work.
void Update (){
if (Input.GetMouseButtonDown (0)) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast (ray, hit))
{
if(hit.transform.name == "Player")
{
Debug.Log ("Logged");
}
}
}
What is wrong with this and how can it be fixed? I am aware that the variable “hit” is nothing at the moment, but I do not know how to assign raycasthit variables. Any help would be greatly appreciated.
Also, it seems that the syntax for Physics.Raycast changed, or I do not understand it properly. In the scripting API it says Raycast(Vector3 origin, Vector3 direction), but most guides on the internet use “hit” as their second parameter. If anyone could explain this, it would be appreciated.