Advertisement
Guest User

PickUpObject.cs

a guest
Nov 4th, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PickUpObject : MonoBehaviour {
  5.    
  6.     private Rigidbody obj;
  7.     public enum CatchState {Empty, Holding, Dropping}
  8.     public CatchState catchState = 0;
  9.     public float throwStrength = 8.0f;
  10.     public float straightenSpeed = 10.0f;
  11.    
  12.     public float reachDist = 4.0f;
  13.     public LayerMask layerMask = -1;
  14.  
  15.     public void DropObject() {
  16.         catchState = CatchState.Dropping;
  17.         print("Dropping");
  18.     }
  19.  
  20.     // Update handles the inputs
  21.     void Update () {
  22.         if(catchState == CatchState.Empty) {
  23.             if(Input.GetButtonDown("Fire2")) {
  24.                 RaycastHit hit;
  25.                 if(Physics.Raycast(transform.position, transform.forward, out hit, reachDist, layerMask)) {
  26.                     PickUp(hit.transform.gameObject);
  27.                     catchState = CatchState.Holding;
  28.                 }
  29.             }
  30.         }
  31.  
  32.         else if(catchState == CatchState.Holding) {
  33.            
  34.             if(Input.GetButton("Drop")) {
  35.                 StartCoroutine(Straighten());
  36.             }
  37.  
  38.             else StopCoroutine("Straighten");
  39.  
  40.             if(Input.GetButtonDown("Fire2")) {
  41.                 catchState = CatchState.Dropping;
  42.             }
  43.         }
  44.  
  45.         if(obj == null) catchState = CatchState.Empty;
  46.  
  47.     }
  48.  
  49.     void FixedUpdate () {
  50.  
  51.         if(catchState == CatchState.Holding) {
  52.             obj.MovePosition(transform.position + transform.forward * 2f);
  53.         }
  54.  
  55.         else if(catchState == CatchState.Dropping) {
  56.             Drop();
  57.             catchState = CatchState.Empty;
  58.         }
  59.     }
  60.  
  61.     void Drop () {
  62.  
  63.         StopCoroutine("Straighten");
  64. /*
  65.         Color c = obj.gameObject.renderer.material.color;
  66.         c.a = 1.0f;
  67.         obj.gameObject.renderer.material.color = c;
  68. */
  69.         obj.gameObject.renderer.material.shader = Shader.Find("HardSurface/Hardsurface Free/Opaque Bump");
  70.  
  71.         // Enable...
  72.  
  73.         // Single gavity
  74.         try {
  75.             obj.GetComponent<SingleGravity>().enabled = true;
  76.         }
  77.         catch {}
  78.  
  79.         // No gravity
  80.         try {
  81.             obj.GetComponent<NoGravity>().enabled = true;
  82.         }
  83.         catch {
  84.             // If there is no NoGravity component, enable gravity
  85.             obj.useGravity = true;
  86.         }
  87.  
  88.         // Anti gravity
  89.         try {
  90.             obj.GetComponent<AntiGravity>().enabled = true;
  91.         }
  92.         catch {}
  93.  
  94.         // Reset the anglular velocity
  95.         obj.angularVelocity = Vector3.zero;
  96.  
  97.         // Add force if not holding the drop button
  98.         if(!Input.GetButton("Drop")) {
  99.             obj.velocity = ((transform.forward) * throwStrength) + transform.parent.rigidbody.velocity * 1.5f;
  100.         }
  101.  
  102.         else {
  103.             obj.velocity = Vector3.zero;
  104.         }
  105.  
  106.         // Remove the reference to the object
  107.         obj = null;
  108.     }
  109.  
  110.     void PickUp (GameObject Object) {
  111.  
  112.         obj = Object.rigidbody;
  113.         obj.useGravity = false;
  114. /*
  115.         Color c = obj.gameObject.renderer.material.color;
  116.         c.a = 0.6f;
  117.         obj.gameObject.renderer.material.color = c;
  118. */     
  119.         obj.gameObject.renderer.material.shader = Shader.Find("HardSurface/Hardsurface Free/Transparent Bump");
  120.  
  121.         try {
  122.             obj.GetComponent<SingleGravity>().enabled = false;
  123.         }
  124.         catch {}
  125.  
  126.         try {
  127.             obj.GetComponent<NoGravity>().enabled = false;
  128.         }
  129.         catch {}
  130.  
  131.         try {
  132.             obj.GetComponent<AntiGravity>().enabled = false;
  133.         }
  134.         catch {}
  135.     }
  136.  
  137.     IEnumerator Straighten() {
  138.         Quaternion start = obj.transform.rotation;
  139.         Quaternion end = Quaternion.Euler(Vector3.zero);
  140.  
  141.         obj.angularVelocity = Vector3.zero;
  142.  
  143.         float t = 0f;
  144.  
  145.         while(obj.transform.rotation != end) {
  146.  
  147.             obj.transform.rotation = Quaternion.Lerp(start, end, t);
  148.  
  149.             t += straightenSpeed;
  150.  
  151.             yield return new WaitForEndOfFrame();
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement