Search Unity

Render Aim Assist Lines

Discussion in 'Scripting' started by Mozes, Oct 8, 2013.

  1. Mozes

    Mozes

    Joined:
    Apr 22, 2013
    Posts:
    42
    Hello all

    I try to add aim assist lines ( Horizontal, Vertical ) crossHair that follow mouse position,
    I did that with LineRenderer, and with GL.line ( OnPostRender ), but the lines lag after the mouse position.
    [video=youtube_share;k7esBRFm83Y]http://youtu.be/k7esBRFm83Y

    In the clip, the white pointer is unity hardware mouse.
    The crosshair lines are gameObjects with linerenderer component that follow mouse position.

    Any Ideas on how to draw the lines 2D or 3D at mouse position and without lag ?

    Thank you :)
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    If you see this clip more attentively - you will see there some lags too. I think, that reason of big lags could be expensive calculations. Coul you post a code of your line drawing here?
     
  3. Mozes

    Mozes

    Joined:
    Apr 22, 2013
    Posts:
    42
    Ok, the crossHair is an empty gameObject with two child gameObjects.
    the parent has a script to follow mouse pos.

    Code (csharp):
    1.  
    2. #pragma strict
    3. private var ray : Ray;
    4. private var hit : RaycastHit;
    5.  
    6. function Start () {
    7.  
    8. }
    9.  
    10. function Update () {
    11.     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    12.     if(Physics.Raycast(ray, hit)){
    13.         transform.position = hit.point;
    14.     }
    15. }
    16.  
    17.  
    The Child objects has a script to add the linerenderer ( one vertical, one horizontal ).
    Code (csharp):
    1.  
    2. #pragma strict
    3. var lineMaterial:Material;
    4. var startSize = 0.2;
    5. var endSize = 0.02;
    6. var c1 : Color = Color.yellow;
    7. var c2 : Color = Color.red;
    8. var lengthOfLineRenderer : int = 2;
    9. private var lineRenderer : LineRenderer;
    10. private var Pos : Vector3;
    11.  
    12.  
    13. function Start(){
    14.     lineRenderer = gameObject.AddComponent(LineRenderer);
    15.     lineRenderer.material = lineMaterial;
    16.     lineRenderer.SetColors(c1, c2);
    17.     lineRenderer.SetWidth( startSize , endSize );
    18.     lineRenderer.SetVertexCount(lengthOfLineRenderer);
    19. }
    20.  
    21. function Update () {
    22.     Pos = Vector3(transform.position.x,transform.position.y,0.0);
    23.     lineRenderer  = gameObject.GetComponent(LineRenderer);
    24.     Pos.x = transform.position.x-100.0;
    25.     lineRenderer.SetPosition(0,Pos);
    26.     Pos.x = transform.position.x+100.0;
    27.     lineRenderer.SetPosition(1,Pos);
    28. }
    29.  
    30.