Strange Line Renderer Bug (Unity)

Updated on May 22, 2018 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
6 on May 21, 2018

Hi, 

I am busy creating a 2d platformer based on the 2d platformer tutorial series by Brackey’s (BIG thank you!). 

I am using a line renderer on a FlameTrail prefab to create the “shooting” effect from the Player weapon to the mousePoint. Pretty much exactly as Brackeys does in the tutorial. 

Everything works as it should and is accurate until the Player jumps up to higher platforms in the same scene/ level. Then the flameTrail goes awry and seems to shoot off in random directions and not to the mousePoint position. 

I have no idea why this is happening and have encountered no such problems on other forums or Unity manual. Please help. 

I am a beginner so if the answer is something simple that I am missing then I apologize! 

thanks! 

  • Liked by
Reply
5 on May 21, 2018

Can we see your code please?

on May 21, 2018

 

Not sure if this is actually where the problem is (or if it’s what you would want to see) but the below is weapon script. 

I can add a short video if that would help? but like I said it works perfectly until the Player climbs higher up on the level :/ 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Weapon : MonoBehaviour {
 
public float fireRate = 0; 
 public int Damage = 10; 
 public LayerMask whatToHit;
 
public Transform FlameTrailPrefab; 
 float timeToSpawnEffect = 0; 
 public float effectSpawnRate = 10; 
 public Transform HitPrefab;
 
// to handle camera shaking 
 public float camShakeAmt = 0.1f; 
 CameraShake camShake;
 
float timeToFire = 0; 
 Transform firePoint;
 
private AudioManager audioManager;
 
// Use this for initialization
 void Awake () {
 firePoint = transform.Find ("FirePoint"); 
 if (firePoint == null) {
 Debug.LogError ("No FirePoint? WHAT??"); 
 }
 
 }
 
void Start () {
 camShake = GameMaster.instance.GetComponent<CameraShake>(); 
 audioManager = AudioManager.instance; 
 if (audioManager == null) {
 Debug.LogError ("NO AUDIO MANAGER FOUND IN THE SCENE"); 
 }
 
}
 
 void FixedUpdate () {
 if (fireRate == 0) {
 if (Input.GetButtonDown ("Fire1")) {
 Shoot ();
 audioManager.PlaySound ("Shoot");
 }
 } else {
 if (Input.GetButton ("Fire1") && Time.time > timeToFire) {
 timeToFire = Time.time + 1 / fireRate; 
 Shoot (); 
 audioManager.PlaySound ("Shoot");
 }
 }
 }
 
void Shoot () {
 Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y); 
 Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y); 
 RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition - firePointPosition, 100, whatToHit);
 
Debug.DrawLine (firePointPosition, mousePosition, Color.green); 
 if (hit.collider != null) {
// Debug.DrawLine (firePointPosition, hit.point, Color.red); 
 Enemy enemy = hit.collider.GetComponent<Enemy> (); 
 // insert hit sound here
 
if (enemy != null) {
 enemy.DamageEnemy (Damage); 
 camShake.Shake (camShakeAmt, 0.2f); 
 // add points for hit score 
 audioManager.PlaySound ("Hit_Hurt5"); 
 }
 }
 
if (Time.time >= timeToSpawnEffect) {
 Vector3 hitPos; 
 Vector3 hitNormal;
 
if (hit.collider == null) {
 hitPos = (mousePosition - firePointPosition) * 20;
 hitNormal = new Vector3 (9999, 9999, 9999); 
 } else {
 hitPos = hit.point; 
 hitNormal = hit.normal; 
 }
 
Effect (hitPos, hitNormal); 
 timeToSpawnEffect = Time.time + 1 / effectSpawnRate; 
 } 
 }
 
void Effect (Vector3 hitPos, Vector3 hitNormal) {
 Transform trail = Instantiate (FlameTrailPrefab, firePoint.position, firePoint.rotation) as Transform;
 trail.parent = firePoint; 
 LineRenderer lr = trail.GetComponent<LineRenderer> ();
 
if (lr != null) {
 lr.SetPosition (0, firePoint.position); 
 lr.SetPosition (1, hitPos); 
 }
 
Destroy (trail.gameObject, 0.02f);
 
if (hitNormal != new Vector3 (9999, 9999, 9999)) {
 Transform hitParticle = Instantiate (HitPrefab, hitPos, Quaternion.FromToRotation (Vector3.right, hitNormal)) as Transform; 
 Destroy (hitParticle.gameObject, 1f);
 }
 
// Method to shake the camera 
// camShake.Shake (camShakeAmt, 0.2f); 
 }
}

 

on May 21, 2018

How is firePoint being moved about the sceen?

on May 21, 2018

The weapon gameobject is the parent. 

on May 21, 2018

Ok, a few things.

I cannot get your code to work on my machine. Not because of error or anything, but because there are SOOOoo many dependencies in this one script, and just setting up a GameObject to use it isn’t worth the effort.

 

I suggest redoing the entire script. Not to be mean or anything. I really don’t know whats causing the bug you explained from your script. When that happens to me, I find just redoing the script often times fixes whatever the hiccup was.

 

I also suggest maybe proof reading some of your code in the future.

 

Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
// is exactly the same as
Vector2 mousePosition =  Camera.main.ScreenToWorldPoint(Input.mousePosition);

 

Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
// is also the same as
Vector2 firePointPosition = firePoint.position; 

 

on May 22, 2018

Yeah :/ I know it’s a bit messy at the moment. And I suspected as much. I’m still learning better ways to use the code so it’s all good. Thanks for your time though. I will post if I find the answer. Dankie! 

Show more replies
  • Liked by
Reply
Cancel