Code Review

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


Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *