Search Unity

How to draw a line (or raycast) straight up from a 2D object and rotate when the object rotates?

Discussion in 'Scripting' started by EliteWalrus, Oct 23, 2014.

  1. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    Code (JavaScript):
    1. function FixedUpdate () {
    2.     Debug.DrawLine (transform.position, transform.TransformDirection(transform.up));
    3. }
    The line for some reason shoots up diagonally to the right and when the object rotates the line starts to rotate more slowly than then the object and then just starts rotating the other way.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I think it is because you are applying transform.TransformDirection() to the transform.up property, which already points "up" relative to the transform you are on.

    You can either do transform.TransformDirection( Vector3.up) or else just use transform.up directly.

    Kurt