comparison Orchestland/Assets/LeapMotion/DemoResources/Scripts/FloatingStem.cs @ 1:f7675884f2a1

Add Orchestland project
author Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2015 23:09:20 +0900
parents
children
comparison
equal deleted inserted replaced
0:347d21cdfc22 1:f7675884f2a1
1 /******************************************************************************\
2 * Copyright (C) Leap Motion, Inc. 2011-2014. *
3 * Leap Motion proprietary. Licensed under Apache 2.0 *
4 * Available at http://www.apache.org/licenses/LICENSE-2.0.html *
5 \******************************************************************************/
6
7 using UnityEngine;
8 using System.Collections;
9
10 public class FloatingStem : MonoBehaviour {
11
12 public float waterHeight = 0.0f;
13 public float transitionWidth = 0.2f;
14 public float waterForce = 0.1f;
15 public float waterTorque = 0.002f;
16 public float waterDrag = 5.0f;
17 public float waterAngularDrag = 10.0f;
18
19 public Vector3 waterCurrentVelocity;
20 public float waterCurrentForce = 0.1f;
21
22 private float air_drag_;
23 private float air_angular_drag_;
24
25 void Start() {
26 air_drag_ = GetComponent<Rigidbody>().drag;
27 air_angular_drag_ = GetComponent<Rigidbody>().angularDrag;
28 }
29
30 void FixedUpdate() {
31 float distanceFromSurface = transform.position.y - waterHeight;
32 if (distanceFromSurface >= 0) {
33 GetComponent<Rigidbody>().drag = air_drag_;
34 GetComponent<Rigidbody>().angularDrag = air_angular_drag_;
35 return;
36 }
37
38 GetComponent<Rigidbody>().drag = waterDrag;
39 GetComponent<Rigidbody>().angularDrag = waterAngularDrag;
40
41 float transition = Mathf.Clamp(-distanceFromSurface / transitionWidth, 0, 1);
42 GetComponent<Rigidbody>().AddForce(new Vector3(0, waterForce * transition, 0));
43
44 /*
45 float dot = Vector3.Dot(transform.up, Vector3.up);
46 Vector3 torque_vector = -dot * Vector3.Cross(transform.up, Vector3.up);
47 rigidbody.AddTorque((1 - transition) * waterTorque * torque_vector);
48 */
49
50 // Current.
51 Vector3 delta_current = waterCurrentVelocity - GetComponent<Rigidbody>().velocity;
52 delta_current.y = 0;
53 GetComponent<Rigidbody>().AddForce(waterCurrentForce * delta_current);
54 }
55 }