Search Unity

Raycast Problem

Discussion in 'Editor & General Support' started by meatcarnage, Jan 26, 2020.

  1. meatcarnage

    meatcarnage

    Joined:
    May 27, 2019
    Posts:
    1
    I am writing a third person shooter and come across a problem that I've spend too much time on.

    Background:
    I am instantiating guns on level load from my external REST API then parenting these guns to the player.

    The guns have a parent model and two children empty game objects
    1.) an empty game object to hold the line renderer (Yellow in Picture) to simulate the bullet tracer
    2.) an empty game object to hold the orthographic texture projector (As the Lasersight also pictured below)

    The Problem: Notice when I select gunEnd and it shows the blue axis, there is both yaw&pitch now applied to the laser comparatively to the Z axes (Forward)



    Code (CSharp):
    1. Debug.DrawLine(gunEnd.transform.position, gunEnd.transform.forward * _range, Color.cyan, 20f);
    This is how I'm drawing the cyan raycast (I have selected the gunEnd in the picture to illustrate the blue axis compared to the drawn ray)
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    The problem is that you're using DrawLine instead of DrawRay. The second argument you're passing is clearly what you intend to be a "direction" vector, but DrawLine expects the second argument to be the position of the endpoint of the line. DrawRay is what you want, which expects the second argument to be the direction.