Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class Inflate: MonoBehaviour {
  6.    
  7.     // List of balloons, can be filled in the inspector
  8.     public List<Rigidbody2D> balloons;
  9.    
  10.     // List of scales to reach
  11.     private List<Vector3> goalScale;
  12.    
  13.     // Speed of the interpolation (Default = 0.1, but can be modified in the inspector)
  14.     public float speed = 0.1f;
  15.    
  16.     // Use this for initialization
  17.     void Start () {
  18.        
  19.         // Initialize the goalScales and attribute to each balloon
  20.         goalScale = new List<Vector3> ();
  21.         for (int i = 0; i<balloons.Count; i++) {
  22.             goalScale.Add(balloons[i].transform.localScale);
  23.         }
  24.     }
  25.    
  26.     // Update is called once per frame
  27.     void Update () {
  28.        
  29.         // Get User Input
  30.         Inputs ();
  31.  
  32.  
  33.         // Apply new scale to each balloon (Lerp to be more fluid)
  34.         for (int i = 0; i<balloons.Count; i++) {
  35.  
  36. //          Degonfle ici
  37.             if (goalScale[i].x > 0.625f) {
  38.            
  39.             //proportional
  40.             goalScale[i] -= goalScale[i]/20 * Time.deltaTime;
  41.  
  42.             //fixed
  43.             // goalScale[i] -= new Vector3 (0.01f,0.1f,0.1f) * Time.deltaTime;
  44.            
  45.             }
  46.  
  47.             balloons[i].transform.localScale = Vector3.Lerp(balloons[i].transform.localScale, goalScale[i], speed);
  48.             balloons[i].mass = balloons[i].transform.localScale.x / 0.15f;
  49.         }
  50.     }
  51.    
  52.     // To inflate
  53.     void InflateBalloon (int balloonNo) {
  54.         // If the balloon is not too big
  55.         if (balloons [balloonNo].transform.localScale.x <= 1.9f) {
  56.             // Change its goal scale
  57.             goalScale [balloonNo] = balloons [balloonNo].transform.localScale + new Vector3 (0.03f, 0.03f, 0.03f);
  58.         }
  59.     }
  60.  
  61.     void Inputs() {
  62.         if (Input.GetKeyDown(KeyCode.W))  {
  63.             // In a list, first element is 0
  64.             InflateBalloon(0);
  65.         }
  66.  
  67.         if (Input.GetKeyDown(KeyCode.M)) {
  68.             InflateBalloon(1);
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement