using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;publicclassDisplayInfo:MonoBehaviour{
publicGameObject displayImage;publicText objectNameText;publicLayerMask mask;privatevoidFixedUpdate(){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hitInfo;if(Physics.Raycast(ray,out hitInfo,200, mask)){
if(hitInfo.collider !=null){
Debug.DrawLine(transform.position, hitInfo.point, Color.red);
displayImage.SetActive(true);
objectNameText.text = hitInfo.collider.gameObject.name;}}else{
objectNameText.text ="";
displayImage.SetActive(false);}}}
using UnityEngine;publicclassExample01:MonoBehaviour{
Ray ray;[SerializeField]privatefloat maxDistance;//Weapon Attack RangepublicLayerMask mask;publicMaterial highlightMat;privatevoidUpdate(){
ray =newRay(transform.position, transform.forward);//Create a new RAY//RaycastHit hitInfo;//CORE STRUCT of ray original & ray direction (Store the information about eray hit the gameObject)//MARKER LayerMaks means we can only SHOT enemy in this layer mask//STEP 01 Single Attack//if(Physics.Raycast(ray, out hitInfo, maxDistance, mask, QueryTriggerInteraction.Ignore)) {
// Debug.Log(hitInfo.collider.gameObject.name);// Debug.DrawLine(transform.position, hitInfo.point, Color.red);//}//else//{
// //Debug.DrawLine(ray.origin, ray.origin + ray.direction * 100, Color.green);// Debug.DrawRay(transform.position, transform.position + transform.forward * 100, Color.yellow);//ONLY view//}//STEP 02
RaycastHit[] hits;
hits = Physics.RaycastAll(ray, maxDistance);
Debug.DrawLine(transform.position, transform.position + transform.forward *20, Color.red);foreach(RaycastHit hit in hits){
hit.collider.gameObject.GetComponent<Renderer>().material = highlightMat;}}}
using System.Collections;using System.Collections.Generic;using UnityEngine;publicclassFire:MonoBehaviour{
publicMaterial highlightMat;privatevoidFixedUpdate(){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;if(Physics.Raycast(ray,out hit,200)){
if(hit.collider !=null){
hit.collider.transform.GetComponent<Renderer>().material = highlightMat;}}}}
using UnityEngine;publicclassPlacement:MonoBehaviour{
//public LayerMask planeMask;//OPTIONAL//public GameObject tree;//public GameObject selectedPrefab;//private void FixedUpdate()//{
// //MARKER Screen Space -> Ray // Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);// //RaycastHit hitInfo;// if(Physics.Raycast(ray, out RaycastHit hitInfo, Mathf.Infinity, planeMask))// {
// tree.transform.position = hitInfo.point;//The Cube will follow our Mouse// tree.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);// if (Input.GetMouseButtonDown(0))// {
// Instantiate(tree, hitInfo.point, Quaternion.FromToRotation(Vector3.up, hitInfo.normal));// }// }//}publicLayerMask planeMask;//OPTIONALpublicGameObject selectedPrefab;privatevoidFixedUpdate(){
//MARKER Screen Space -> Ray Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//RaycastHit hitInfo;if(Physics.Raycast(ray,outRaycastHit hitInfo, Mathf.Infinity, planeMask)){
selectedPrefab.transform.position = hitInfo.point;//The Cube will follow our Mouse
selectedPrefab.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);if(Input.GetMouseButtonDown(0)){
Instantiate(selectedPrefab, hitInfo.point, Quaternion.FromToRotation(Vector3.up, hitInfo.normal));}}}}
using System.Collections;using System.Collections.Generic;using UnityEngine;publicclassTreeButton:MonoBehaviour{
publicGameObject treePrefab;publicvoidPressButton(){
FindObjectOfType<Placement>().selectedPrefab = treePrefab;}}