「Unityゲーム開発 オンライン3Dアクションゲームの作り方」
上記の本の序盤に「プレイヤー」というゲームオブジェクトにCharacterControllerコンポーネントをつけ、
「CharacterMove」というスクリプトを追加して動かす、という章があるのですが、そのまま使うと
動いてくれません。
そこで、サンプルコードのStart関数の下記場所をコメントアウトしたところ、
移動するようになりました。
lang
1 destination = transform.position;
(↑だとプレイヤー位置=目的地になってしまうため、arrivedがtrueとなり、
移動しないと考えた)
その後、Mecanimeでアニメーションを生成する際に詰まり、
ここだけサンプルコードをいじっているので、コメントアウトがまずかったのか、
気になっています。
環境はUnity5、4.6両方で試しました。
下はサンプルそのままのコードです。
lang
1using UnityEngine; 2using System.Collections; 3 4// キャラクターを移動させる。 5// Chapter3 6public class CharacterMove : MonoBehaviour { 7 // 重力値. 8 const float GravityPower = 9.8f; 9 // 目的地についたとみなす停止距離. 10 const float StoppingDistance = 0.6f; 11 12 // 現在の移動速度. 13 Vector3 velocity = Vector3.zero; 14 // キャラクターコントローラーのキャッシュ. 15 CharacterController characterController; 16 // 到着したか(到着した true/到着していない false) 17 public bool arrived = false; 18 19 // 向きを強制的に指示するか. 20 bool forceRotate = false; 21 22 // 強制的に向かせたい方向. 23 Vector3 forceRotateDirection; 24 25 // 目的地. 26 public Vector3 destination; 27 28 // 移動速度. 29 public float walkSpeed = 6.0f; 30 31 // 回転速度. 32 public float rotationSpeed = 360.0f; 33 34 35 36 // Use this for initialization 37 void Start () { 38 characterController = GetComponent<CharacterController>(); 39 destination = transform.position; 40 } 41 42 // Update is called once per frame 43 void Update () { 44 45 // 移動速度velocityを更新する 46 if (characterController.isGrounded) { 47 // 水平面での移動を考えるのでXZのみ扱う. 48 Vector3 destinationXZ = destination; 49 destinationXZ.y = transform.position.y;// 高さを目的地と現在地を同じにしておく. 50 51 //********* ここからXZのみで考える. ******** 52 // 目的地までの距離と方角を求める. 53 Vector3 direction = (destinationXZ - transform.position).normalized; 54 float distance = Vector3.Distance(transform.position,destinationXZ); 55 56 // 現在の速度を退避. 57 Vector3 currentVelocity = velocity; 58 59 // 目的地にちかづいたら到着.. 60 if (arrived || distance < StoppingDistance) 61 arrived = true; 62 63 64 // 移動速度を求める. 65 if (!arrived) 66 velocity = Vector3.zero; 67 else 68 velocity = direction * walkSpeed; 69 print(velocity); 70 71 72 // スムーズに補間. 73 velocity = Vector3.Lerp(currentVelocity, velocity,Mathf.Min (Time.deltaTime * 5.0f ,1.0f)); 74 velocity.y = 0; 75 76 77 if (!forceRotate) { 78 // 向きを行きたい方向に向ける. 79 if (velocity.magnitude > 0.1f && !arrived) { // 移動してなかったら向きは更新しない. 80 Quaternion characterTargetRotation = Quaternion.LookRotation(direction); 81 transform.rotation = Quaternion.RotateTowards(transform.rotation,characterTargetRotation,rotationSpeed * Time.deltaTime); 82 } 83 } else { 84 // 強制向き指定. 85 Quaternion characterTargetRotation = Quaternion.LookRotation(forceRotateDirection); 86 transform.rotation = Quaternion.RotateTowards(transform.rotation,characterTargetRotation,rotationSpeed * Time.deltaTime); 87 } 88 89 90 } 91 92 // 重力. 93 velocity += Vector3.down * GravityPower * Time.deltaTime; 94 95 // 接地していたら思いっきり地面に押し付ける. 96 // (UnityのCharactorControllerの特性のため) 97 98 Vector3 snapGround = Vector3.zero; 99 if (characterController.isGrounded) 100 snapGround = Vector3.down; 101 102 // CharacterControllerを使って動かす. 103 characterController.Move(velocity * Time.deltaTime+snapGround); 104 if (characterController.velocity.magnitude < 0.1f) 105 arrived = true; 106 107 // 強制的に向きを変えるを解除. 108 if (forceRotate && Vector3.Dot(transform.forward,forceRotateDirection) > 0.99f) 109 forceRotate = false; 110 111 112 } 113 114 // 目的地を設定する.引数destinationは目的地. 115 public void SetDestination(Vector3 destination) 116 { 117 arrived = false; 118 this.destination = destination; 119 } 120 121 // 指定した向きを向かせる. 122 public void SetDirection(Vector3 direction) 123 { 124 forceRotateDirection = direction; 125 forceRotateDirection.y = 0; 126 forceRotateDirection.Normalize(); 127 forceRotate = true; 128 } 129 130 // 移動をやめる. 131 public void StopMove() 132 { 133 destination = transform.position; // 現在地点を目的地にしてしまう. 134 } 135 136 // 目的地に到着したかを調べる. true 到着した/ false 到着していない. 137 public bool Arrived() 138 { 139 return arrived; 140 } 141 142 143} 144
一応、オブジェクトの設定を載せます。
lang
1■■Player■■ 2Transform(130, 0, -40) 3Rotation(0, 0, 0) 4Scale(1, 1, 1) 5 6●CharacterController 7Slope Limit:20 8Step Offset:0.3 9Skin Width:0.05 10Min Move Distance:0.001 11Center(0, 1, 0) 12Radius:0.5 13Height:2 14 15■■Terrain■■ 16Transform(100, 0, -100) 17Rotation(0, 0, 0) 18Scale(1, 1, 1) 19
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。