Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can't produce lines using Line Renderer.

Discussion in 'Scripting' started by NidoAnxari, Jul 22, 2017.

  1. NidoAnxari

    NidoAnxari

    Joined:
    Jan 2, 2017
    Posts:
    86
    I'm currently working on a 2D bubble shooter like game. Where I want an aim line to help player aim. I'm using Line Renderer to do so. Following is the code I'm using to set positions:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class testAimLine : MonoBehaviour
    6. {
    7.     public GameObject obj;
    8.     LineRenderer lr;
    9.  
    10.     [SerializeField]
    11.     public int numberOfReglections;
    12.  
    13.     Vector3[] pos = new Vector3[5];
    14.     Vector3[] rot = new Vector3[5];
    15.  
    16.     private void Start()
    17.     {
    18.         lr = GetComponent<LineRenderer>();
    19.  
    20.         pos[0] = gameObject.transform.position;
    21.         rot[0] = gameObject.transform.eulerAngles;
    22.  
    23.         lr.SetPosition(0, pos[0]);
    24.     }
    25.    
    26.     private void Update()
    27.     {
    28.         for (int i = 1; i < numberOfReglections; i++)
    29.         {
    30.             RaycastHit2D ray = Physics2D.Raycast(pos[i - 1], rot[i - 1]);
    31.  
    32.             pos[i] = ray.point;
    33.             rot[i] = Vector3.Reflect(pos[i], ray.normal);
    34.  
    35.             lr.SetPosition(i, pos[i]);
    36.         }
    37.     }
    38. }
    39.  
    But due to some unknown reasons it is not working. I'm almost new to coding so be easy on me ;)
     
  2. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    if you start your index from 1, try yo change i <= numberOfReglections

    When you use LineRenderer, if you want to display it on some canvas, change the "Render Mode" of the Canvas in "Screen Space - Camera"

    Change this also in: pos = ray.point + Vector3.forward * 1;

    and this: lr.SetPosition(0, pos[0] + Vector3.forward * 1);

    If this all dont work, assing a new material to the line renderer, and change the shader in Sprite/Default
     
  3. BakeMyCake

    BakeMyCake

    Joined:
    May 8, 2017
    Posts:
    175
    Is there any special reason why you multiply a forward vector by one? Wouldn't that result in the same thing?
     
  4. NidoAnxari

    NidoAnxari

    Joined:
    Jan 2, 2017
    Posts:
    86
    This is the simplified prototype script.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class testAimLine_1 : MonoBehaviour
    6. {
    7.     public GameObject testObj;
    8.  
    9.     private void Update()
    10.     {
    11.         RaycastHit2D hit0 = Physics2D.Raycast(transform.position, transform.up);
    12.         Vector2 reflection = Vector2.Reflect(hit0.point, hit0.normal);
    13.  
    14.         RaycastHit2D hit1 = Physics2D.Raycast(hit0.point, reflection);
    15.  
    16.         testObj.transform.position = hit1.point;
    17.  
    18.         Debug.DrawLine(transform.position, hit0.point, Color.red);
    19.         Debug.DrawLine(hit0.point, hit1.point, Color.blue);
    20.     }
    21. }
    22.  
    This is the screenshot:
    Red is the first line but blue line that should be the reflected one is absent.
     
  5. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    yes, if the line is generated without distance from the camera, maybe the line isn't show for the clipping plane Near of the camera
     
  6. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    here there is not any Line Renderer
     
  7. NidoAnxari

    NidoAnxari

    Joined:
    Jan 2, 2017
    Posts:
    86
    Because line renderer is not causing problem. Problem is with Ray casting.
     
  8. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    Maybe Hit1 is null beacause it dont hit anything
     
  9. NidoAnxari

    NidoAnxari

    Joined:
    Jan 2, 2017
    Posts:
    86
    I also tried if statement but nothing happened.
     
  10. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    But what do you want to do esactly?
     
  11. NidoAnxari

    NidoAnxari

    Joined:
    Jan 2, 2017
    Posts:
    86
    I want to make an aim line like in puzzle bobble
     
  12. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    Ahhhh, ok i understand the error: you display the blue line on the top of the red one, in reverse:

    Change this with
    Code (CSharp):
    1. RaycastHit2D hit1 = Physics2D.Raycast(hit0.point, reflection + Vector2.right * 90);
     
  13. NidoAnxari

    NidoAnxari

    Joined:
    Jan 2, 2017
    Posts:
    86
    I will try it. Hope it works. ☺
     
  14. NidoAnxari

    NidoAnxari

    Joined:
    Jan 2, 2017
    Posts:
    86
    Anyway thanks a lot.
     
  15. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    let me know :D
     
  16. NidoAnxari

    NidoAnxari

    Joined:
    Jan 2, 2017
    Posts:
    86
    Alas, it didn't work.
     
  17. NidoAnxari

    NidoAnxari

    Joined:
    Jan 2, 2017
    Posts:
    86
    I am thinking that my whole concept is wrong. But can't find another solution.
     
  18. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
  19. NidoAnxari

    NidoAnxari

    Joined:
    Jan 2, 2017
    Posts:
    86
    I'll try.
     
  20. NidoAnxari

    NidoAnxari

    Joined:
    Jan 2, 2017
    Posts:
    86
    It didn't work.
     
  21. NidoAnxari

    NidoAnxari

    Joined:
    Jan 2, 2017
    Posts:
    86
    Did you tried the code yourself?
     
  22. NidoAnxari

    NidoAnxari

    Joined:
    Jan 2, 2017
    Posts:
    86
    Now when I rotated the gameObject blue line which was the reflected ray appeared. But it appears in random rotations. Sometimes it appears and sometimes doesn't.
     
  23. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    Ok, it's something, try to pass me the entire new code, maybe we can adjust it
     
  24. NidoAnxari

    NidoAnxari

    Joined:
    Jan 2, 2017
    Posts:
    86
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class testAimLine_1 : MonoBehaviour
    6. {
    7.     private void FixedUpdate()
    8.     {
    9.  
    10.         RaycastHit2D hit0 = Physics2D.Raycast(transform.position, transform.up);
    11.      
    12.         if (hit0)
    13.         {
    14.             Debug.DrawLine(transform.position, hit0.point, Color.red);
    15.            
    16.             Vector2 reflection = Vector2.Reflect(hit0.point, hit0.normal);
    17.  
    18.             RaycastHit2D hit1 = Physics2D.Raycast(hit0.point, reflection);
    19.          
    20.            
    21.             if(hit1)
    22.             {
    23.                 Debug.DrawLine(hit0.point, hit1.point, Color.blue);
    24.                
    25.             }
    26.         }
    27.         print("point" + hit0.point + "normal " + hit0.normal);
    28.     }
    29. }
    30.  
     
  25. NidoAnxari

    NidoAnxari

    Joined:
    Jan 2, 2017
    Posts:
    86
    This is now driving me crazy. I have tried everything but this problem is not going anywhere.
     
  26. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    Ok, i tryied some changes, try this
    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.    
    5.     public class testAimLine_1 : MonoBehaviour
    6.     {
    7.        
    8.         public GameObject obj;
    9.        
    10.         private void FixedUpdate()
    11.         {
    12.    
    13.             RaycastHit2D hit0 = Phisics2D.Raycast(transform.position, transform.up);
    14.        
    15.             if (hit0.collider != null)
    16.             {
    17.                 Debug.DrawLine(transform.position, hit0.point, Color.red);
    18.                
    19.                 Vector2 reflection = new Vector2(hit.point.x - transform.position.x, hit.point.y - transform.position.y);
    20.                
    21.                 Raycast hit1;
    22.                
    23.                 if(reflection.x > 0)
    24.                 {
    25.                     //because the reflection in pointing backwards, and you want to point it up, and in this case if hit in the left size
    26.                     hit1 = Physics2D.Raycast(hit0.point, reflection - 90f);
    27.                 }
    28.                 else
    29.                 {
    30.                     //the same thing, but reverse
    31.                     hit1 = Physics2D.Raycast(hit0.point, reflection + 90f);
    32.                 }
    33.  
    34.                 if(hit1.collider != null)
    35.                 {
    36.                     Debug.DrawLine(hit0.point, hit1.point, Color.blue);
    37.                     obj.transform.position = hit1.point;
    38.                 }
    39.                 else
    40.                 {
    41.                     Debug.Log("Second Hit don't finded");
    42.                     obj.transform.position = hit0.point;
    43.                 }
    44.             }
    45.             else
    46.             {
    47.                 Debug.LogWarning("Nothing hitted");
    48.             }
    49.         }
    50.     }