Raycast in Unity

  • 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

--

--

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