slimabob

DragRigidbody.cs

Nov 11th, 2015
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5.  
  6. namespace UnityStandardAssets.Utility
  7. {
  8.     public class DragRigidbody1 : NetworkBehaviour
  9.     {
  10.         const float k_Spring = 50.0f;
  11.         const float k_Damper = 5.0f;
  12.         const float k_Drag = 10.0f;
  13.         const float k_AngularDrag = 5.0f;
  14.         const float k_Distance = 0.2f;
  15.         const bool k_AttachToCenterOfMass = false;
  16.  
  17.         private SpringJoint m_SpringJoint;
  18.  
  19.         RaycastHit hit = new RaycastHit();
  20.  
  21.         private void Update()
  22.         {
  23.             // Make sure the user pressed the mouse down
  24.             if (!Input.GetMouseButtonDown(0))
  25.             {
  26.                 return;
  27.             }
  28.  
  29.             var mainCamera = FindCamera();
  30.  
  31.             // We need to actually hit an object
  32.             if (
  33.                 !Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition).origin, mainCamera.ScreenPointToRay(Input.mousePosition).direction, out hit, 100, Physics.DefaultRaycastLayers))
  34.             {
  35.                 return;
  36.             }
  37.             // We need to hit a rigidbody that is not kinematic
  38.             if (!hit.rigidbody || hit.rigidbody.isKinematic)
  39.             {
  40.                 return;
  41.             }
  42.  
  43.             if (!m_SpringJoint)
  44.             {
  45.                 var go = new GameObject("Rigidbody dragger");
  46.                 Rigidbody body = go.AddComponent<Rigidbody>();
  47.                 m_SpringJoint = go.AddComponent<SpringJoint>();
  48.                 body.isKinematic = true;
  49.             }
  50.  
  51.             m_SpringJoint.transform.position = hit.point;
  52.             m_SpringJoint.anchor = Vector3.zero;
  53.  
  54.             m_SpringJoint.spring = k_Spring;
  55.             m_SpringJoint.damper = k_Damper;
  56.             m_SpringJoint.maxDistance = k_Distance;
  57.             m_SpringJoint.connectedBody = hit.rigidbody;
  58.  
  59.             StartCoroutine("DragObject", hit.distance);
  60.         }
  61.  
  62.         IEnumerator DragObject(float distance)
  63.         {
  64.             var oldDrag = m_SpringJoint.connectedBody.drag;
  65.             var oldAngularDrag = m_SpringJoint.connectedBody.angularDrag;
  66.             m_SpringJoint.connectedBody.drag = k_Drag;
  67.             m_SpringJoint.connectedBody.angularDrag = k_AngularDrag;
  68.             var mainCamera = FindCamera();
  69.             while (Input.GetMouseButton(0))
  70.             {
  71.                 var ray = mainCamera.ScreenPointToRay(Input.mousePosition);
  72.                 m_SpringJoint.transform.position = ray.GetPoint(distance);
  73.                 yield return null;
  74.             }
  75.             if (m_SpringJoint.connectedBody)
  76.             {
  77.                 m_SpringJoint.connectedBody.drag = oldDrag;
  78.                 m_SpringJoint.connectedBody.angularDrag = oldAngularDrag;
  79.                 m_SpringJoint.connectedBody = null;
  80.             }
  81.         }
  82.  
  83.  
  84.         private Camera FindCamera()
  85.         {
  86.             if (GetComponent<Camera>())
  87.             {
  88.                 return GetComponent<Camera>();
  89.             }
  90.  
  91.             return Camera.main;
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment