- Home /
Aim Line (Line Renderer) not showing in 2D shooter
I'm a Unity novice and I'm creating a small shooting game on a school project and I already managed to have a functional shooting system in the main character that even bounces off some objects using Unity Physics. I now wanted to integrate an aim line that will predict the trajectory of the bullet, including the bounces on objects (bubble-shooter style). I found a script that uses Raycast and Line Renderer and it supposedly does this and I tried to integrate it into my gun script but although it does not give any error, it simply does not show anything when I test the game. I do not know if the problem is in the settings that I put in the Line Renderer Component or it is in the Script. Could someone help me understand where my error is and point the right way?
My goal is:
My Line Renderer Component Definitions:
My Weapon Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(LineRenderer))]
public class Weapon : MonoBehaviour
{
[Range(1, 5)]
[SerializeField] private int _maxIterations = 3;
[SerializeField] private float _maxDistance = 10f;
public int _count;
public LineRenderer _line;
public Transform Firepoint;
public GameObject BulletPrefab;
public GameObject FirePrefab;
void Start()
{
_line = GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
_count = 0;
_line.SetVertexCount(1);
_line.SetPosition(0, transform.position);
_line.enabled = RayCast(new Ray(transform.position, transform.forward));
}
void Shoot()
{
//shooting logic
var destroyBullet = Instantiate(BulletPrefab, Firepoint.position, Firepoint.rotation);
Destroy(destroyBullet, 10f);
var destroyFire = Instantiate(FirePrefab, Firepoint.position, Firepoint.rotation);
Destroy(destroyFire, 0.3f);
}
private bool RayCast(Ray ray)
{
RaycastHit hit;
if (Physics.Raycast(ray, out hit, _maxDistance) && _count <= _maxIterations - 1)
{
_count++;
var reflectAngle = Vector3.Reflect(ray.direction, hit.normal);
_line.SetVertexCount(_count + 1);
_line.SetPosition(_count, hit.point);
RayCast(new Ray(hit.point, reflectAngle));
return true;
}
_line.SetVertexCount(_count + 2);
_line.SetPosition(_count + 1, ray.GetPoint(_maxDistance));
return false;
}
}
@titovelosa So I tried out your script and line options on my project and it seemed to work just fine... One way to debug is to check if when you play your line renderer is enabled or not. In your script on line 35 you could just remove the _line.enabled =
and that would make the line show up even if it wasn't hitting. Then you could at least let you narrow it down to if the line isn't rendering at all or if your script isn't triggering it properly. But yeah I'm a little stumped because it is working on my test.
@rballard , i will try to see that when im back home...i see that your project was 3D....$$anonymous$$e are 2D, perhaps something about that?
Line Renderer works in 2D, but you'll have to make sure that the z-pos makes it in front of everything (like a background) so that the camera will see it.
I copied his code and it works, the only thing I changed was the warnings related to linerenderer.setposition. I imagine your problem is that your raycast is hitting nothing so it isn't being displayed or it is hitting the things that you are instanciating.
Answer by Crumpet · Feb 07, 2019 at 05:24 AM
What part doesn't work? You're only displaying the line if it hits something?
this is your code working fine from what I can see, I haven't changed anything.
Yes, the code don't have errors but in my test the line is not showing. Perhaps because my project is 2d?!
thats not posible, share screenshots of the game view while on play mode and the inspector. i see the linerenderer is inside the weapon, and might be causing some conflicts because how line renderer works, can you try using that exact script in a object with his transform reseted to 0? if you tell your line renderer go from point 0,0,0 to 0,0,50 but your line renderer is in the point 10,15,10 for example, it will go from the 10,15,10 to the 10,15,60. take this into account.
Hi, @xxmariofer ,here the screenshots. I want a aim line attached to firepoint, before shooting.
Answer by titovelosa · Feb 07, 2019 at 02:16 PM
Hi, all! Look like Unity has two physics engines one for 2D and one for 3D. The script relies on the 3D physics engine and won't work for 2D colliders. I edited the script to function with 2D colliders. Also the original script only shows the line renderer if it hits something so i made some suggested changes to fix this. So now the script looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(LineRenderer))]
public class Weapon : MonoBehaviour
{
[Range(1, 5)]
[SerializeField] private int _maxIterations = 3;
[SerializeField] private float _maxDistance = 10f;
public int _count;
public LineRenderer _line;
public Transform Firepoint;
public GameObject BulletPrefab;
public GameObject FirePrefab;
private void Start()
{
_line = GetComponent<LineRenderer>();
}
// Update is called once per frame
private void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
_count = 0;
_line.SetVertexCount(1);
_line.SetPosition(0, transform.position);
_line.enabled = true;
RayCast(transform.position, transform.up);
}
private void Shoot()
{
//shooting logic
var destroyBullet = Instantiate(BulletPrefab, Firepoint.position, Firepoint.rotation);
Destroy(destroyBullet, 10f);
var destroyFire = Instantiate(FirePrefab, Firepoint.position, Firepoint.rotation);
Destroy(destroyFire, 0.3f);
}
private bool RayCast(Vector2 position, Vector2 direction)
{
RaycastHit2D hit = Physics2D.Raycast(position, direction, _maxDistance);
if (hit && _count <= _maxIterations - 1)
{
_count++;
var reflectAngle = Vector2.Reflect(direction, hit.normal);
_line.SetVertexCount(_count + 1);
_line.SetPosition(_count, hit.point);
RayCast(hit.point + reflectAngle, reflectAngle);
return true;
}
if (hit == false)
{
_line.SetVertexCount(_count + 2);
_line.SetPosition(_count + 1, position + direction * _maxDistance);
}
return false;
}
}
Now I already have the aim line but I will have to make some adjustments. The aim line does not point in the direction of the gun and it also seems to me that the trajectory of the bullet is different from the trajectory of the aim line, right?
Game Gif: https://ibb.co/MDsMRM0
I think transform.forward is still just a vector3 (0,0,1) by the look of it your x axis is forward so it'd need to be (1,0,0) which I think is transform.right. Not sure if that is what you were looking for but I will check it out in 2D later if you're still having problems I didn't even think about 2D being the issue
transform.forward is not (0,0,1) thats vector3.forward they are different
yes but it is 0,0,1 which is on the z axis depending on where the transform is looking, whereas vector3.forward is straight along the z axis doesn't matter what way the transform would be looking right?