75 lines
1.6 KiB
C#
75 lines
1.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Mouse : MonoBehaviour
|
|
{
|
|
Rigidbody2D rig;
|
|
new CircleCollider2D collider2D;
|
|
public bool iscut;
|
|
Vector2 lastPos;
|
|
Camera cam;
|
|
public float minspeedcut;
|
|
|
|
public GameObject linePrefab;
|
|
private GameObject currentline;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
cam = Camera.main;
|
|
rig = GetComponent<Rigidbody2D>();
|
|
collider2D = GetComponent<CircleCollider2D>();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if(Input.GetMouseButtonDown(0))
|
|
{
|
|
StartCut();
|
|
}
|
|
else if(Input.GetMouseButtonUp(0))
|
|
{
|
|
EndCut();
|
|
}
|
|
|
|
if(iscut)
|
|
{
|
|
UpdateCut();
|
|
}
|
|
}
|
|
|
|
void UpdateCut()
|
|
{
|
|
Vector2 newPos = cam.ScreenToWorldPoint(Input.mousePosition);
|
|
rig.position = newPos;
|
|
|
|
float velocity = (newPos - lastPos).magnitude * Time.deltaTime;
|
|
|
|
if(velocity > minspeedcut)
|
|
{
|
|
collider2D.enabled = true;
|
|
}
|
|
else
|
|
{
|
|
collider2D.enabled = false;
|
|
}
|
|
}
|
|
|
|
void StartCut()
|
|
{
|
|
iscut = true;
|
|
currentline = Instantiate(linePrefab, transform);
|
|
collider2D.enabled = true;
|
|
lastPos = cam.ScreenToWorldPoint(Input.mousePosition);
|
|
}
|
|
|
|
void EndCut()
|
|
{
|
|
iscut = false;
|
|
currentline.transform.SetParent(null);
|
|
Destroy(currentline, 5);
|
|
collider2D.enabled = false;
|
|
}
|
|
}
|