キーボードでFingalone を動かすためのプログラムを作ります。Assets で右クリック、Create > C# Script を選びます。
C# をダブルクリックすると、Visual Studio が起動します。キーボードでFingalone を動かすために以下のプログラムを作ってセーブ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class control_pattern : MonoBehaviour { public GameObject Gameobject1; public GameObject Gameobject2; public GameObject Gameobject3; public GameObject Gameobject4; private HingeJoint joint; private Rigidbody rb1; private Vector3 init_pos1; private Quaternion init_rot1; private Rigidbody rb2; private Vector3 init_pos2; private Quaternion init_rot2; private Rigidbody rb3; private Vector3 init_pos3; private Quaternion init_rot3; private Rigidbody rb4; private Vector3 init_pos4; private Quaternion init_rot4; private float dA = 5; private HingeJoint hinge1; private float A1 = 0; private float A1_min = -90; private float A1_max = 90; private HingeJoint hinge2; private float A2 = 0; private float A2_min = -90; private float A2_max = 90; private HingeJoint hinge3; private float A3 = 0; private float A3_min = -90; private float A3_max = 90; private float[] pat_f1 = { 0, -120, -150 }; private float[] pat_f2 = { 0, -40, 90 }; private float[] pat_f3 = { 0, 0, 0 }; private int i_f = 0; private int n_f = 3; private float[] pat_b1 = { -110, -110, 0 }; private float[] pat_b2 = { 90, 0, 0 }; private float[] pat_b3 = { 0, 0, 0 }; private int i_b = 0; private int n_b = 3; private float[] pat_l1 = { 0, -40, -40, 0 }; private float[] pat_l2 = { 90, 0, 0, 90 }; private float[] pat_l3 = { -60, -60, 60, 60 }; private int i_l = 0; private int n_l = 4; private float[] pat_r1 = { 0, -40, -40, 0 }; private float[] pat_r2 = { 90, 0, 0, 90 }; private float[] pat_r3 = { 60, 60, -60, -60 }; private int i_r = 0; private int n_r = 4; // Start is called before the first frame update void Start() { rb1 = Gameobject1.GetComponent<Rigidbody>(); init_pos1 = rb1.transform.position; init_rot1 = rb1.transform.rotation; rb2 = Gameobject2.GetComponent<Rigidbody>(); init_pos2 = rb2.transform.position; init_rot2 = rb2.transform.rotation; rb3 = Gameobject3.GetComponent<Rigidbody>(); init_pos3 = rb3.transform.position; init_rot3 = rb3.transform.rotation; rb4 = Gameobject4.GetComponent<Rigidbody>(); init_pos4 = rb4.transform.position; init_rot4 = rb4.transform.rotation; hinge1 = Gameobject1.GetComponent<HingeJoint>(); hinge2 = Gameobject2.GetComponent<HingeJoint>(); hinge3 = Gameobject3.GetComponent<HingeJoint>(); } // Update is called once per frame void ChangePos() { JointSpring hingeSpring = hinge1.spring; hingeSpring.targetPosition = A1; hinge1.spring = hingeSpring; JointSpring hingeSpring2 = hinge2.spring; hingeSpring2.targetPosition = A2; hinge2.spring = hingeSpring2; JointSpring hingeSpring3 = hinge3.spring; hingeSpring3.targetPosition = A3; hinge3.spring = hingeSpring3; } void Update() { if (Input.GetKeyDown(KeyCode.E)) { i_f += 1; if (i_f >= n_f) i_f = 0; A1 = pat_f1[i_f]; A2 = pat_f2[i_f]; A3 = pat_f3[i_f]; ChangePos(); } if (Input.GetKeyDown(KeyCode.D)) { i_b += 1; if (i_b >= n_b) i_b = 0; A1 = pat_b1[i_b]; A2 = pat_b2[i_b]; A3 = pat_b3[i_b]; ChangePos(); } if (Input.GetKeyDown(KeyCode.S)) { i_l += 1; if (i_l >= n_l) i_l = 0; A1 = pat_l1[i_l]; A2 = pat_l2[i_l]; A3 = pat_l3[i_l]; ChangePos(); } if (Input.GetKeyDown(KeyCode.F)) { i_r += 1; if (i_r >= n_r) i_r = 0; A1 = pat_r1[i_r]; A2 = pat_r2[i_r]; A3 = pat_r3[i_r]; ChangePos(); } if (Input.GetKey(KeyCode.Alpha1)) { A1 = 0; A2 = 0; A3 = 0; ChangePos(); } if (Input.GetKey(KeyCode.Alpha2)) { A1 = -90; A2 = 90; A3 = 0; ChangePos(); } if (Input.GetKey(KeyCode.Alpha3)) { A1 = -90; A2 = -10; A3 = 0; ChangePos(); } if (Input.GetKey(KeyCode.U)) { A1 += dA; if (A1 > A1_max) A1 = A1_max; ChangePos(); } if (Input.GetKey(KeyCode.J)) { A1 -= dA; if (A1 < A1_min) A1 = A1_min; ChangePos(); } if (Input.GetKey(KeyCode.I)) { A2 += dA; if (A2 > A2_max) A2 = A2_max; ChangePos(); } if (Input.GetKey(KeyCode.K)) { A2 -= dA; if (A2 < A2_min) A2 = A2_min; ChangePos(); } if (Input.GetKey(KeyCode.O)) { A3 += dA; if (A3 > A3_max) A3 = A3_max; ChangePos(); } if (Input.GetKey(KeyCode.L)) { A3 -= dA; if (A3 < A3_min) A3 = A3_min; ChangePos(); } if (Input.GetKey(KeyCode.Space)) { rb4.velocity = new Vector3(0, 3, 0); } if (Input.GetKey(KeyCode.UpArrow)) { rb4.transform.Translate(-0.1f, 0.0f, 0.0f); } if (Input.GetKey(KeyCode.DownArrow)) { rb4.transform.Translate(0.1f, 0.0f, 0.0f); } if (Input.GetKey(KeyCode.LeftArrow)) { rb4.transform.Rotate(0.0f, -5.0f, 0.0f); } if (Input.GetKey(KeyCode.RightArrow)) { rb4.transform.Rotate(0.0f, 5.0f, 0.0f); } Vector3 pos = rb4.transform.position; if (pos.y < -10) { rb1.transform.position = init_pos1; rb1.transform.rotation = init_rot1; rb1.velocity = new Vector3(0, 0, 0); rb2.transform.position = init_pos2; rb2.transform.rotation = init_rot2; rb2.velocity = new Vector3(0, 0, 0); rb3.transform.position = init_pos3; rb3.transform.rotation = init_rot3; rb3.velocity = new Vector3(0, 0, 0); rb4.transform.position = init_pos4; rb4.transform.rotation = init_rot4; rb4.velocity = new Vector3(0, 0, 0); } // controller.Move(movedirection * Time.deltaTime); } } |
動かし方は
[u, j] 第一関節のモーター
[i, k] 第二関節のモーター
[o, l] 第三関節のモーター
[e, d, s, f]の連打モーターを使った前後・旋回移動
[1, 2, 3] 特定のポーズ
[↑, ↓, ← ,→]前後・旋回移動(モーターを使わない、インチキ移動)
[space] 浮遊
C#のプログラム名をcontrol_patternとする。プログラム内でのclass 名とファイル名が一致していないとエラーが出るので注意。このプログラムをFinger1にドラッグすると、右側のInspector にcontrol_pattern が追加される。その欄にGameObject1-4 があるので、◎をクリックして、Finger1-4 を割り当てました。
この状態で、カーソルキーでFingeronを動かせることはできますが、関節を動かすにはJointの物理特性を変更する必要があります。以下のように、Finger1のHinge Joint で、Use Spring にチェックを入れ、バネの強さ Spring 700, バネの遅さ Damper 50 とセット。Finger2もFinger3も同じようにセットしました。
あとは、きちんと動くように物理パラメータを調節しました。Finger4 が軽すぎて動きの反動が大きいので、Rigidbodyのmass を5として重くしました。
あとは、Finger1の先端の摩擦を強くして、進みやすくします。Assets で右クリック、Create > Physic Material を作成します。”ゴム”と命名しました。
作成した”ゴム”を選択し、Dynamic Friction = 1、Static Friction = 1とセットして摩擦を最大にしました。
Finger1 の BoxCollider のMaterial に、◎をクリックしてさっき作った”ゴム”を選択しました。
同様にして、Finger4は、つるつるにしました。
やったことは以上です。これでキーボード操作で自由にFingalone が動きました。