Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Blade : MonoBehaviour
- {
- public Vector3 velocity {
- get;
- private set;
- }
- //other stuff in the class
- void UpdateCut() {
- Vector2 newPosition = cam.ScreenToWorldPoint(Input.mousePosition);
- rb.position = newPosition;
- //here is what's changed
- velocity = (newPosition - previousPosition);
- float distance = velocity.magnitude;
- //everything else is the same
- float speed = distance / Time.deltaTime;
- if (speed > minCuttingVelocity) {
- col.enabled = true;
- } else {
- col.enabled = false;
- }
- previousPosition = newPosition;
- }
- }
- //the other script
- public class Fruit : MonoBehaviour
- {
- //other stuff
- void OnTriggerEnter2D(Collider2D col) {
- if (col.tag == "Blade") {
- //changed stuff
- Blade blade = col.GetComponent<Blade>();
- Vector3 direction = ((blade != null) ? blade.velocity : col.transform.position - transform.position).normalized;
- //everything else is the same
- Quaternion rotation = Quaternion.LookRotation(direction);
- GameObject slicedFruit = Instantiate(fruitSlicedPrefab, transform.position, rotation);
- Destroy(gameObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement