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

Education and Certification

Useful Linux Commands — Files and Packages

Bridging the Gap: Scrum and Agile Development as it relates to the 7 Roles

What is Jenkins? And Its Use-Cases

Keep it Simple & Secure — 1

5 CSS Tricks I Bet You Didn’t Know

How to Stand Out From the Competition When You Are a Self-Taught Developer

Contributing to open source: Get comfortable being uncomfortable. Then do the robot.

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

Setting up a platformer project in Unity

Mr Fixit — Adding a Repair Powerup

Tiling a Procedurally Generated Unity TileMap

Light her up — Adding emissions to your game objects using photoshop and blender