Raycast in Unity

  • Reference to the main camera
  • Check if lmb is pressed
  • If so, create a new Vector3 that will set the values of the camera’s center position
    The camera has a property called Viewport. The viewport represents the width and the height of the screen with values from 0 to 1, so if we want to indicate the center, the coordinates will be 0,5 and 0,5
  • Create a ray from the center of the camera
  • Create a RaycastHit hitInfo
    This variable will store the information about the object hit
  • Declare a Physics.Raycast method that will return a bool value
    If that method returns true, the code inside it will be executed, for example, the information about the gameobject hit

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Migrate ASP.NET Core 2.2 system to ASP.NET Core 3.1

How Quitting My Job And Joining A Coding Bootcamp Changed My Life — And How It Can Change Yours Too

Avoid The Three Mistakes When Using a Map in Go

IDamagable Interface in Unity

Migration Approaches from On-Premises to Cloud Environment

Thinking of learning to code? That’s the easy bit. Here’s how to tackle the hard parts.

Transforming developer from commodity to premium — A tale of Microservices

Emulating Azure Storage with Azurite

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hernando Nieto Jaramillo

Hernando Nieto Jaramillo

Unity developer

More from Medium

Benefits of Prototyping Without Assets

My journey becoming a Unity game developer: 2.5D Infinite Runner-Ledge Grab System-Pt4

Set Lasers To Destroy

Adding colliders to a scene