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. Dismiss Notice

Vector Lengths

Discussion in 'Scripting' started by matthewseaward, May 9, 2014.

  1. matthewseaward

    matthewseaward

    Joined:
    Apr 12, 2013
    Posts:
    50
    Hi,

    I've been working on a basic laser sight (using a line renderer) for my 2.5D style side shooter.

    Using the code below I've calculated the end point of the sight based upon the mouse location (and limited it so the player can't do a full flip) and it works.

    Code (csharp):
    1.         GetComponent<LineRenderer>().SetPosition(0, new Vector3(transform.position.x, transform.position.y, transform.position.z));
    2.         Vector3 mousePosition = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, Input.mousePosition.z - camera.transform.position.z));
    3.         if (mousePosition.x <transform.position.x+10) mousePosition.x = transform.position.x+10;
    4.         Vector3 pointTo = new Vector3(mousePosition.x,mousePosition.y,  transform.position.z);     
    5.         GetComponent<LineRenderer>().SetPosition(1, pointTo);
    However, Instead of the end point ending where the players mouse is, I want the line to continue off the screen. I've used the scale method but the result is undesired. The line is a long way offset from where the bullet is going.

    Code to scale:
    Code (csharp):
    1. pointTo.Scale(new Vector3(2f,2f,1f));
    (I wanted to keep the z on it's current position)
    $Untitled.jpg
     
    Last edited: May 9, 2014
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    how did you try this? did you account for the fact the line isn't starting at the origin?
     
  3. matthewseaward

    matthewseaward

    Joined:
    Apr 12, 2013
    Posts:
    50
    I've edited the post to show my code useage of the scale function.

    No I didn't...how would I do that?
     
  4. Wibber

    Wibber

    Joined:
    Dec 27, 2012
    Posts:
    12
    Can't you just multiply the vector?

    Code (csharp):
    1.  
    2. pointTo *= 2f;
    3.  
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Just multiplying pointTo will, as has been hinted, just move it further away from the origin (0,0,0), NOT further away from the player. You will need to subtract the player's position (giving you an "offset" vector, that is, the direction the line is moving). THEN you can multiply it by whatever. After the multiplication, add the player's position back into it.
     
  6. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    I get the feeling this probably isn't what you really want though. This will always cause the laser to be double the distance that was specified, but what it sound like you really want is a laser that's a set length (of a little more than the maximum distance on screen). That method may require some trig to get the angle though.

    StarManta has a point in that you need to make sure to keep things relative.
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    If you want a fixed length, then you'll want to do the same thing I suggested, except you use theOffsetVector.normalized, and then multiply that by your desired length.
     
  8. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Code (csharp):
    1. GetComponent<LineRenderer>().SetPosition(1, transform.position + (PointTo - transform.position).normalized * distance);
     
  9. matthewseaward

    matthewseaward

    Joined:
    Apr 12, 2013
    Posts:
    50
    Thanks for all of your suggestions. I now understand how to get it working :D

    This code worked perfectly - thanks!
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Glad it worked. You should consider splitting complex lines like that up into several lines, though - if you have a problem in the future, separate lines will make it 10x easier to pinpoint to cause of the error.