Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to rotate DrawLine with an object?

Discussion in 'Scripting' started by TRG96, Sep 13, 2013.

  1. TRG96

    TRG96

    Joined:
    Mar 26, 2011
    Posts:
    102
    I have a cube and I am using Debug.DrawLine(transform.position, vector3.forward * 10, Color.red);

    This is the results I get

    $Untitled.png

    When the cube rotates, the line does not rotate with the cube. It is always pointing in the global Z axis.

    A long time ago I used LookAt function and debug.DrawLine and it was working without implementing any other code for rotating the line with the cube. I have no idea why its not working now =(
     
  2. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I believe you want lookAt instead of vector3.forward.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, you'd use transform.forward. However Debug.DrawRay would be more appropriate.

    --Eric
     
    normand likes this.
  4. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    When do you use lookAt Eric?
     
  5. TRG96

    TRG96

    Joined:
    Mar 26, 2011
    Posts:
    102
    DrawRay works
     
  6. CaJerry87

    CaJerry87

    Joined:
    Oct 31, 2016
    Posts:
    3
    So what is the end code? Because Debug.DrawRay(transform.position, vector3.forward, Color.red); does that same thing.
     
  7. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,390
    Vector3.forward is a static vector (0,0,1). You have to use MySillyCubeTransform.TransformDirection(Vector3.forward) to make it a direction relative to the cube.
     
  8. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    These will also do the same thing:
    Code (CSharp):
    1. Debug.DrawRay(transform.position, transform.forward * distance, Color.red);
    2. Debug.DrawLine(transform.position, transform.position + (transform.forward * distance), Color.red);
     
    Last edited: Jun 28, 2018
    Gafanha likes this.