Search Unity

How to add aim lines in Unity

Discussion in 'Scripting' started by Vishal Ved, Feb 6, 2016.

  1. Vishal Ved

    Vishal Ved

    Joined:
    Dec 2, 2014
    Posts:
    1
    I want to add aim lines like the images shown below in Unity. I also want to show the direction in which the pool ball and the cue ball will go when I shoot the ball.

    The below images show what I want to achieve.

    http://postimg.org/image/8thc5ypvp/
    http://postimg.org/image/boufcttvp/

    The arrows in the above images are aim lines.

    Can anyone of you let me know how to achieve that.
     
  2. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
  3. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    Well for the rendering of the line the Line Renderer is the best way to do so, but it looks like your also wanting ot find the reflection angle as well so that can be done like this.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TargetLine : MonoBehaviour {
    4.     private LineRenderer _line;
    5.     [SerializeField] private float _reflectDistance = 2f;
    6.  
    7.     private void Start() {
    8.         _line = GetComponent<LineRenderer>();
    9.         _line.SetVertexCount(3);
    10.     }
    11.  
    12.     private void Update() {
    13.         Ray ray = new Ray(transform.position, transform.forward);
    14.         RaycastHit hit;
    15.         if (Physics.Raycast(ray, out hit)) {
    16.             Vector3 reflectAngle = Vector3.Reflect(ray.direction, hit.normal) * _reflectDistance;
    17.             _line.SetPositions(new[] {transform.position, hit.point, reflectAngle});
    18.         }
    19.     }
    20. }
    21.  
    just some basic raycasting but the important part is the Vector3.Reflect() which you feed the direction of the ray as well as the normal of the surface the ray hit and it will give you the angle of the reflection.

    Also if you wanted multiple reflects a recursive method could be used like so.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TargetLine : MonoBehaviour {
    4.     [Range(1, 5)]
    5.     [SerializeField] private int _maxIterations = 3;
    6.     [SerializeField] private float _maxDistance = 10f;
    7.  
    8.     private int _count;
    9.     private LineRenderer _line;
    10.  
    11.     private void Start() {
    12.         _line = GetComponent<LineRenderer>();
    13.     }
    14.  
    15.     private void Update() {
    16.         _count = 0;
    17.         _line.SetVertexCount(1);
    18.         _line.SetPosition(0, transform.position);
    19.         _line.enabled = RayCast(new Ray(transform.position, transform.forward));
    20.     }
    21.  
    22.     private bool RayCast(Ray ray) {
    23.         RaycastHit hit;
    24.         if (Physics.Raycast(ray, out hit, _maxDistance) && _count <= _maxIterations - 1) {
    25.             _count++;
    26.             var reflectAngle = Vector3.Reflect(ray.direction, hit.normal);
    27.             _line.SetVertexCount(_count + 1);
    28.             _line.SetPosition(_count, hit.point);
    29.             RayCast(new Ray(hit.point, reflectAngle));
    30.             return true;
    31.         }
    32.         _line.SetVertexCount(_count + 2);
    33.         _line.SetPosition(_count + 1, ray.GetPoint(_maxDistance));
    34.         return false;
    35.     }
    36. }
    37.  
     
    Last edited: Feb 6, 2016
    Thincc, roointan and alanyau1613 like this.