Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@SenpaiRar
Created May 1, 2019 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SenpaiRar/f369aa2663297ba38bef28da306cb97c to your computer and use it in GitHub Desktop.
Save SenpaiRar/f369aa2663297ba38bef28da306cb97c to your computer and use it in GitHub Desktop.
How the game handles the player clicking on the screen
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Cursor : MonoBehaviour {
public Camera main_Camera;
public GameObject Player;
// Update is called once per frame
void Start(){
Cursor.visible = true; By default, the player can't see their cursor. So we just make it visible.
}
void Update () {
ClickOn();
}
void ClickOn()
{
if (Input.GetMouseButtonUp(1))
{
Ray camera_Ray = main_Camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit_Ray;
Physics.Raycast(camera_Ray, out hit_Ray);
ClickableParent C = hit_Ray.collider.gameObject.GetComponent();
C.OnClick(Player.transform.position);
}
}
public static Vector3 GetPositionClick(Camera C) //returns the point where the raycast hit on the ground
{
Ray camera_Ray = C.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Physics.Raycast(camera_Ray, out hit);
return (hit.point);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment