#Raycasting in unity | #Ray cast hit detection unity c# in less than 1 min | Code in description

Subscribers:
1,110
Published on ● Video Link: https://www.youtube.com/watch?v=auckMzgfPdk



Duration: 0:57
157 views
10


using UnityEngine;
/*
Logic : A ray is cast in forward direction. If it hits anything color of ray changes to yellow
else it will be white. You can perform the activity on hit detection
*/
public class RaycastHitdetection : MonoBehaviour
{
////// it is better to perform physics related work in fixed updated rather than update ////////
void FixedUpdate()
{

RaycastHit hit;

// Syntax : Physics.Raycast(Vector3 startPosition,Vector3 direction e.f transform.forward/up, out hit, float distance));
bool hasHit= Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity);

// Does the ray intersect any objects
if (hasHit==true)
{
////////// this simply draws a ray from transform object till the object it has hit /////////
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
Debug.Log("Did Hit");
}
else
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
Debug.Log("Did not Hit");
}
}
}




Other Videos By Muhammad Shahzaib


2022-06-26Train physics package demo
2022-06-25Realistic car head lights in unity3d easy tutorial
2022-06-20Invoke method after time gap easy way Unity 3d | Invoke and coroutines in unity
2022-06-19Police Siren in unity3d easy tutorial
2022-06-17Set JFrame title and Resize JFrame | Learn Java GUI
2022-06-15Download and install unity tutorial
2022-06-04Copy Text To Clip board Unity | #CopyClipboard in unity
2022-05-29Cloth Physics in Unity 3d | Flag Example included | Step by step easy tutorial
2022-05-21Ayat al kursi | Ayat ul kursi with beautiful background and soothing sound| #Quran
2022-04-24#Cargo Parking| #My new released game| download link in description
2022-04-22#Raycasting in unity | #Ray cast hit detection unity c# in less than 1 min | Code in description
2022-04-10Make transparent material | #Unity3d| #Transparent material in unity3d
2022-03-19HMS Gui update and add patients data entry | Other controls (Clock,alarm, cutomization,notes) added
2022-03-13Drag a 3-D object using mouse unity| Leran unity | Drag in unity | Unity screen to world tutorial
2022-03-07Rotate a game object around other Unity | Rotate Arount unity| Learn unity | Unity rotation
2022-02-16Make a custom sky/sky box from image in unity3d| Unity skybox development from scratch|Learn unity3d
2022-02-06Hospital Management System Using Java and SQL Server Demo
2022-01-30Shafqat Mehmood at GCU Lahore | Shafqat Mehmood funny speech
2022-01-24Heavy rain in punjab village| Beautiful rain falling in winter season
2022-01-23Cyclic Redundancy Check Demo | Software demo on Cyclic Redundancy Check | CRC
2022-01-16Sura e Baqara Ayat 259-260 Urdu/Hindi Translation | beautiful Quran Translation | Quran recitation