Trail Renderer tracking problem
Hey everyone!
I'm pretty new to Unity, and I followed a tutorial to create a replica of the Fruit Ninja game, and it included a Trail Renderer for registering the blade cuts.
An empty GameObject instantiates BladeTrail objects, which have a trail renderer on them, and then destroys them. It's difficult to describe, but my problem is that, even though these instances are supposed to be separate objects, they constantly start at the ending point of their predecessor rather than immediately from mouse position (this video shows it pretty clearly).
Even with the exact code the YouTuber used, I get this problem. Does anyone have a solution to this? Here's the code on the Empty Object that instantiates the cuts(which have a trail renderer on them):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Blade : MonoBehaviour {
bool isCutting = false;
public GameObject bladeTrailPrefab;
GameObject currentBladeTrail;
Rigidbody2D rb;
Camera cam;
void Start()
{
cam = Camera.main;
rb = GetComponent<Rigidbody2D>();
}
void Update () {
if (Input.GetMouseButtonDown(0))
{
StartCutting();
} else if (Input.GetMouseButtonUp(0)) {
StopCutting();
}
if (isCutting) {
UpdateCut();
}
}
void UpdateCut () {
rb.position = cam.ScreenToWorldPoint(Input.mousePosition);
}
void StartCutting () {
isCutting = true;
currentBladeTrail = Instantiate(bladeTrailPrefab, transform);
}
void StopCutting () {
isCutting = false;
currentBladeTrail.transform.SetParent(null);
Destroy(currentBladeTrail, 2f);
}
}
Thanks for anyone taking the time!