Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PickUpObject : MonoBehaviour {
- private Rigidbody obj;
- public enum CatchState {Empty, Holding, Dropping}
- public CatchState catchState = 0;
- public float throwStrength = 8.0f;
- public float straightenSpeed = 10.0f;
- public float reachDist = 4.0f;
- public LayerMask layerMask = -1;
- public void DropObject() {
- catchState = CatchState.Dropping;
- print("Dropping");
- }
- // Update handles the inputs
- void Update () {
- if(catchState == CatchState.Empty) {
- if(Input.GetButtonDown("Fire2")) {
- RaycastHit hit;
- if(Physics.Raycast(transform.position, transform.forward, out hit, reachDist, layerMask)) {
- PickUp(hit.transform.gameObject);
- catchState = CatchState.Holding;
- }
- }
- }
- else if(catchState == CatchState.Holding) {
- if(Input.GetButton("Drop")) {
- StartCoroutine(Straighten());
- }
- else StopCoroutine("Straighten");
- if(Input.GetButtonDown("Fire2")) {
- catchState = CatchState.Dropping;
- }
- }
- if(obj == null) catchState = CatchState.Empty;
- }
- void FixedUpdate () {
- if(catchState == CatchState.Holding) {
- obj.MovePosition(transform.position + transform.forward * 2f);
- }
- else if(catchState == CatchState.Dropping) {
- Drop();
- catchState = CatchState.Empty;
- }
- }
- void Drop () {
- StopCoroutine("Straighten");
- /*
- Color c = obj.gameObject.renderer.material.color;
- c.a = 1.0f;
- obj.gameObject.renderer.material.color = c;
- */
- obj.gameObject.renderer.material.shader = Shader.Find("HardSurface/Hardsurface Free/Opaque Bump");
- // Enable...
- // Single gavity
- try {
- obj.GetComponent<SingleGravity>().enabled = true;
- }
- catch {}
- // No gravity
- try {
- obj.GetComponent<NoGravity>().enabled = true;
- }
- catch {
- // If there is no NoGravity component, enable gravity
- obj.useGravity = true;
- }
- // Anti gravity
- try {
- obj.GetComponent<AntiGravity>().enabled = true;
- }
- catch {}
- // Reset the anglular velocity
- obj.angularVelocity = Vector3.zero;
- // Add force if not holding the drop button
- if(!Input.GetButton("Drop")) {
- obj.velocity = ((transform.forward) * throwStrength) + transform.parent.rigidbody.velocity * 1.5f;
- }
- else {
- obj.velocity = Vector3.zero;
- }
- // Remove the reference to the object
- obj = null;
- }
- void PickUp (GameObject Object) {
- obj = Object.rigidbody;
- obj.useGravity = false;
- /*
- Color c = obj.gameObject.renderer.material.color;
- c.a = 0.6f;
- obj.gameObject.renderer.material.color = c;
- */
- obj.gameObject.renderer.material.shader = Shader.Find("HardSurface/Hardsurface Free/Transparent Bump");
- try {
- obj.GetComponent<SingleGravity>().enabled = false;
- }
- catch {}
- try {
- obj.GetComponent<NoGravity>().enabled = false;
- }
- catch {}
- try {
- obj.GetComponent<AntiGravity>().enabled = false;
- }
- catch {}
- }
- IEnumerator Straighten() {
- Quaternion start = obj.transform.rotation;
- Quaternion end = Quaternion.Euler(Vector3.zero);
- obj.angularVelocity = Vector3.zero;
- float t = 0f;
- while(obj.transform.rotation != end) {
- obj.transform.rotation = Quaternion.Lerp(start, end, t);
- t += straightenSpeed;
- yield return new WaitForEndOfFrame();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement