Search Unity

Line Renderer doesn't go to SetPosition 1

Discussion in 'Scripting' started by winterfive, Aug 13, 2018.

  1. winterfive

    winterfive

    Joined:
    May 21, 2018
    Posts:
    31
    I'm having trouble with my line renderer component. The renderer is placed on my drone prefab so each instance has it's own (plz see image).



    The lasers being fired by the drones should reach the camera (aka the player, this is for VR). Here's my code (class ShootBeam, placed on the drone prefab GunTip object).

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class ShootBeam : MonoBehaviour {
    5.  
    6.     public float laserDuration;
    7.  
    8.     private LineRenderer _line;
    9.     private Vector3 _camPosition;
    10.     private Transform _gunTipTransform;
    11.     private WaitForSeconds _laserDuration;
    12.  
    13.     private void Start()
    14.     {
    15.         _line = this.GetComponent<LineRenderer>();
    16.         _line.enabled = false;
    17.         _camPosition = Camera.main.gameObject.transform.position;
    18.         _gunTipTransform = this.gameObject.transform;
    19.         _laserDuration = new WaitForSeconds(laserDuration);
    20.     }
    21.  
    22.     private void Update()
    23.     {
    24.         if (Input.GetButton("Fire1"))
    25.         {
    26.             StopCoroutine("ShootAtPlayer");
    27.             StartCoroutine("ShootAtPlayer");
    28.         }
    29.     }
    30.  
    31.     private IEnumerator ShootAtPlayer()
    32.     {
    33.         // play drone laser fire sound TODO
    34.         _line.enabled = true;
    35.      
    36.         _line.SetPosition(0, _gunTipTransform.position);
    37.         _line.SetPosition(1, _camPosition);
    38.  
    39.         yield return laserDuration;
    40.         _line.enabled = false;
    41.     }
    42. }
    43.  
    Edit: I have my camera clipping planes at 0.01 and 80. This more then covers my scene geometry. Culling Mask is set to Everything.

    I'm only using the Input.GetButton() for testing. Once I fix the ray length issue, I'm changing it to fire randomly based on player visibility to the drone and a random amount of time.

    Thanks!
     
    Last edited: Aug 13, 2018
  2. Gambit-MSplitz

    Gambit-MSplitz

    Joined:
    May 16, 2015
    Posts:
    1,277
    You record your camera position as "_camPosition" in the Start() function. The Start() function only gets run once, but your camera will probably never stop moving since this is a VR game. So, your "_camPosition" will always represent the start position of the camera unless you update it.

    I recommend saving a reference to "_camTransform" instead of "_camPosition", like you have done with the gun tip transform.

    Good luck!
     
    winterfive likes this.
  3. winterfive

    winterfive

    Joined:
    May 21, 2018
    Posts:
    31
    I've made the camera stationary (position wise). The camera can rotate along with the player's head (up/down, l/r). The drones and their GunTip objects are constantly moving though. I should have mentioned that. I have another class that handles rotating the drone turrets to be constantly looking at the player.
     
  4. Gambit-MSplitz

    Gambit-MSplitz

    Joined:
    May 16, 2015
    Posts:
    1,277
    Ok, a couple more things to check:
    - Make sure your LineRenderer has "Use World Space" set to true since you are not using local positions
    - Make sure your player camera and ONLY your player camers has the GameObject tag "MainCamera" so you access it using "Camera.main"
     
    winterfive likes this.
  5. winterfive

    winterfive

    Joined:
    May 21, 2018
    Posts:
    31
    If I use World Space, there are no lines at all in either the game view or scene view. I only have one camera. Even though your suggestions aren't working, I appreciate the replies!
     
  6. Gambit-MSplitz

    Gambit-MSplitz

    Joined:
    May 16, 2015
    Posts:
    1,277
    I think it'll be hard for me to help you much more without seeing the problem myself.

    I do strongly recommend you "use world space" on your LineRenderer because you use "transform.position" which is a world-space position, and not "transform.localPosition" which is an object-space position (also called local-space).

    Maybe try Debug.DrawLine() so you can see the line between your two points without any unknown logic getting in the way. (Debug.DrawLine() can only be seen in scene view, not game view)
     
    winterfive likes this.
  7. winterfive

    winterfive

    Joined:
    May 21, 2018
    Posts:
    31
    I'll read up on World vs Local (I have only a basic understanding of it). I did have a debug line going in earlier versions and it worked perfectly. It all seems pretty straightforward: draw a line between point a and point b. Thanks very much for your input!
     
unityunity