Search Unity

LineRenderer: How to make it follow the mouse in the opposite direction?

Discussion in 'Scripting' started by Henoc, May 31, 2020.

  1. Henoc

    Henoc

    Joined:
    Aug 25, 2014
    Posts:
    7
    I'm trying to develop a football game with a drag and launch input and its working really good, but the LineRenderer in this case is not helping at all



    the red line is a Debug.DrawRay
    Code (CSharp):
    1. Debug.DrawRay(transform.position, -forceVector / magMultiplier, currentColor);
    My goal is to copy this draw ray logic and apply it to the LineRendererComponent (which is the purple line)

    so when the gameobject is clicked, it shows this purple line around it mimicking the mouse

    (this is a drag and shoot system)
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    So LineRenderer behaves a bit closer to Debug.DrawLine than Debug.DrawRay, where it takes absolute world positions rather than one absolute position and one relative one.

    DrawRay takes an origin point, and a direction. The end of the ray will actually be at origin + direction. So you probably want something like:
    Code (CSharp):
    1. // However you get your LR reference...
    2. LineRenderer lineRenderer;
    3.  
    4. lineRenderer.positionCount = 2;
    5. // Line starts at the cue ball...
    6. lineRenderer.SetPosition(0, transform.position);
    7.  
    8. // And ends at cueball + force
    9. Vector3 force = -forceVector / magMultiplier;
    10. lineRenderer.SetPosition(1, transform.position + force);
     
  3. Henoc

    Henoc

    Joined:
    Aug 25, 2014
    Posts:
    7
    @PraetorBlue

    tried you code and the result is this:

    upload_2020-5-31_14-36-57.png

    At the moment this is the current config for the LineRenderer component

    upload_2020-5-31_14-37-55.png

    I used your logic before and I'm pretty sure it should work, maybe this is a bug with the LineRenderer component?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Try turning on the "Use World Space" checkbox on that inspector
     
    Henoc likes this.
  5. Henoc

    Henoc

    Joined:
    Aug 25, 2014
    Posts:
    7
    @PraetorBlue it worked! really weird I tried this same code, but not creating a new vector3 object, adding it inside the function directly and it did some weird results! Thanks so much!
     
    PraetorBlue likes this.