Hi,
Im following this tutorial: How to make Fruit Ninja in Unity (Complete Tutorial) 🍉🔪 - YouTube for fruit ninja game.
The trail does not get cleared even after I leave the left mouse button. Here is my complete code until now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Blade : MonoBehaviour
{
private Camera mainCamera;
private Collider bladeCollider;
private TrailRenderer bladeTrail;
private bool slicing;
public Vector3 direction { get; private set; }
public float minSliceVelocity = 0.01f; //minimum speed of the blade
private void Awake()
{
mainCamera = Camera.main;
bladeCollider = GetComponent<Collider>();
bladeTrail = GetComponentInChildren<TrailRenderer>();
}
private void OnEnable()
{
StopSlicing();
}
private void OnDisable()
{
StopSlicing();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
StartSlicing();
}else if (Input.GetMouseButtonDown(0))
{
StopSlicing();
}else if (slicing)
{
ContinueSlicing();
}
}
private void StartSlicing()
{
Vector3 newPosition = mainCamera.ScreenToWorldPoint(Input.mousePosition);//converting from screen space to world space
newPosition.z = 0f;//setting the blade to be at the origin
transform.position = newPosition;
slicing = true;
bladeCollider.enabled = true;
bladeTrail.enabled = true;
bladeTrail.Clear();
}
private void StopSlicing()
{
slicing= false;
bladeCollider.enabled = false;
bladeTrail.enabled = false;
}
private void ContinueSlicing()
{
Vector3 newPosition= mainCamera.ScreenToWorldPoint(Input.mousePosition);//converting from screen space to world space
newPosition.z = 0f;//setting the blade to be at the origin
direction = newPosition - transform.position;//direction the blade is moving in
//determine how fast the blade is moving
float velocity = direction.magnitude / Time.deltaTime;
bladeCollider.enabled = velocity> minSliceVelocity;
transform.position = newPosition;
}
}