Guest User

Untitled

a guest
Jan 15th, 2014
154
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class test_grab : MonoBehaviour {
  5.     public Transform player_camera;
  6.     public Transform phys_dragger;
  7.     public float grab_distance = 5.0f;
  8.     private float temp_drag = 5.0f;
  9.  
  10.     void Update () {
  11.         if(Input.GetButtonDown("Interact")){
  12.             RaycastHit hit;
  13.             if(Physics.Raycast(player_camera.position, player_camera.forward, out hit, grab_distance)){
  14.                 if(hit.rigidbody){
  15.                 phys_dragger.transform.position = hit.rigidbody.position;
  16.                 phys_dragger.transform.rotation = hit.rigidbody.rotation;
  17.                     StartCoroutine(DragObject(hit.rigidbody));
  18.                 }
  19.             }
  20.         }
  21.     }
  22.    
  23.     IEnumerator DragObject(Rigidbody r){
  24.         float oldDrag = r.drag;
  25.         float oldAngularDrag = r.angularDrag;
  26.         r.drag = temp_drag;
  27.         r.angularDrag = temp_drag_ang;
  28.         r.useGravity = false;
  29.         while(Input.GetButton("Interact")){
  30.             r.transform.rotation = phys_dragger.rotation;
  31.             Vector3 vel = (phys_dragger.position - r.position).normalized;
  32.             float d = Mathf.Min(Vector3.Distance(r.transform.position, phys_dragger.position), 1.0f);
  33.             r.AddForce(vel * d, ForceMode.VelocityChange);
  34.             if(Input.GetButton("Throw")){
  35.                 r.rigidbody.AddForce(player_camera.transform.forward * 5, ForceMode.Impulse);
  36.                 r.drag = oldDrag;
  37.                 r.angularDrag = oldAngularDrag;
  38.                 r.useGravity = true;
  39.                 yield break;
  40.             }
  41.             yield return new WaitForFixedUpdate();
  42.         }
  43.         r.drag = oldDrag;
  44.         r.angularDrag = oldAngularDrag;
  45.         r.useGravity = true;
  46.     }
  47. }
RAW Paste Data