Advertisement
Guest User

Brackeys Fruit Ninja Velocity-Based Slicing

a guest
Aug 21st, 2017
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. public class Blade : MonoBehaviour
  2. {
  3.    
  4.     public Vector3 velocity {
  5.         get;
  6.         private set;
  7.     }
  8.    
  9.     //other stuff in the class
  10.    
  11.     void UpdateCut() {
  12.         Vector2 newPosition = cam.ScreenToWorldPoint(Input.mousePosition);
  13.         rb.position = newPosition;
  14.        
  15.         //here is what's changed
  16.         velocity = (newPosition - previousPosition);
  17.         float distance = velocity.magnitude;
  18.         //everything else is the same
  19.         float speed = distance / Time.deltaTime;
  20.         if (speed > minCuttingVelocity) {
  21.             col.enabled = true;
  22.         } else {
  23.             col.enabled = false;
  24.         }
  25.  
  26.         previousPosition = newPosition;
  27.     }
  28.    
  29. }
  30.  
  31. //the other script
  32. public class Fruit : MonoBehaviour
  33. {
  34.    
  35.     //other stuff
  36.    
  37.     void OnTriggerEnter2D(Collider2D col) {
  38.         if (col.tag == "Blade") {
  39.             //changed stuff
  40.             Blade blade = col.GetComponent<Blade>();
  41.             Vector3 direction = ((blade != null) ? blade.velocity : col.transform.position - transform.position).normalized;
  42.             //everything else is the same
  43.             Quaternion rotation = Quaternion.LookRotation(direction);
  44.            
  45.             GameObject slicedFruit = Instantiate(fruitSlicedPrefab, transform.position, rotation);
  46.             Destroy(gameObject);
  47.         }
  48.     }
  49.    
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement