Reputation: 986
im trying to make a mouse trail. So when you hold the mouse button down, it starts the trail and you can move around and then when you release, it dissapers. Im using the trail renderer for this.
Im tryna to replica the blade thats seen in stuff like fruit ninja. So i have an empty game object called blade with a kinematic rigidbody 2d and my blade script.
I then have a trail which is a prefab that i drag into the blade script. Here is the blade script:
bool isCutting = false;
Rigidbody2D rb;
Camera cam;
public GameObject trail;
GameObject currentTrail;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
cam = Camera.main;
}
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
isCutting = true;
currentTrail = Instantiate(trail, transform);
} else if (Input.GetMouseButtonUp(0))
{
isCutting = false;
currentTrail.transform.SetParent(null);
}
if (isCutting)
{
rb.position = cam.ScreenToWorldPoint(Input.mousePosition);
Destroy(currentTrail, 2f);
}
}
The only problem is, when i hold my mouse down, the blade insanely teleports to my mouse position with the trail renderer.
So i think the default blade position is in the middle of the camera, if i drag and hold at the top, u can see the beginning of a blade is a straight line trail from the middle to the top and i want the trail to start where i click.
I hope that makes sense. Thanks
Upvotes: 1
Views: 80
Reputation: 3108
Try changing the last section of the Update()
method to:
if (isCutting)
{
currentTrail.transform.position = rb.position = cam.ScreenToWorldPoint(Input.mousePosition);
Destroy(currentTrail, 2f);
}
Upvotes: 3