teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

画像 プログラムコードを追加しました

2019/11/01 13:41

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,2 +1,239 @@
1
1
  キャラクターがある一定の角度を超えるとすり抜けてしまう現象が起きてしまいます!
2
- もしよろしければ対処方やコードを教えてもらえると幸いです!
2
+ もしよろしければ対処方やコードを教えてもらえると幸いです!
3
+ using System. Collections;
4
+ using System.Collections.Generic;
5
+ using UnityEngine;
6
+ public class situmonyou: MonoBehaviour
7
+ {
8
+ [SerializeField]
9
+ private CapsuleCollider charaCon;
10
+ private Animator animCon; // アニメーションするための変数
11
+ private Vector3 moveDirection; // 移動する方向とベクトル(動く力、速度)の変数(最初は初期化しておく)
12
+ // レイを飛ばす体の位置
13
+ [SerializeField]
14
+ private Transform charaRay;
15
+ // レイの距離
16
+ [SerializeField]
17
+ private float charaRayRange = 0.2f;
18
+ // レイが地面に到達しているかどうか
19
+ private bool isGround;
20
+ // rigidbody
21
+ private Rigidbody rigid;
22
+ private bool isGroundCollider = false;
23
+ // 段差を昇る為のレイを飛ばす位置
24
+ [SerializeField]
25
+ private Transform stepRay;
26
+ // レイを飛ばす距離
27
+ [SerializeField]
28
+ private float stepDistance = 0.5f;
29
+ // 昇れる段差
30
+ [SerializeField]
31
+ private float stepOffset = 0.3f;
32
+ // 昇れる角度
33
+ [SerializeField]
34
+ private float slopeLimit = 65f;
35
+ // 昇れる段差の位置から飛ばすレイの距離
36
+ [SerializeField]
37
+ private float slopeDistance = 1f;
38
+ // ステップアップする力
39
+ [SerializeField]
40
+ private float stepUpPower = 1.5f;
41
+ // ヒットした情報を入れる場所
42
+ private RaycastHit stepHit;
43
+ //public float:インスペクタで調整可能な変数
44
+ [SerializeField]
45
+ private float idoSpeed = 1.5f; // 移動速度
46
+ public float rotateSpeed = 3.0F; // 向きを変える速度
47
+ public float kaitenSpeed = 1200.0f; // プレイヤーの回転速度
48
+ public float gravity = 10.0F; //重力の強さ
49
+ [SerializeField]
50
+ public float jumpPower = 5f; //ジャンプのスピード
51
+ // キャラの回転スピード
52
+ [SerializeField]
53
+ private float charaRotateSpeed = 45f;
54
+ public Vector3 groundNormal = Vector3.zero;
55
+ private Vector3 lastGroundNormal = Vector3.zero;
56
+ [System.NonSerialized]
57
+ public Vector3 lastHitPoint = new Vector3(Mathf.Infinity, 0, 0);
58
+ protected float groundAngle = 0;
59
+ // 地面から離れたとする距離
60
+ public float distanceToTheGround = 2.5f;
61
+ // 着地アニメーションへと変わる距離
62
+ public float distanceToLanding = 2.5f;
63
+ // 地面との距離を計る為のレイを飛ばす位置
64
+ private Transform spine;
65
+ void Start()
66
+ {
67
+ animCon = GetComponent<Animator>(); // アニメーターのコンポーネントを参照する
68
+ moveDirection = Vector3.zero;
69
+ myStatus = GetComponent<MyStatus>();
70
+ spine = animCon.GetBoneTransform(HumanBodyBones.Spine);
71
+ isGround = false;
72
+ rigid = GetComponent<Rigidbody>();
73
+ state = MyState.Normal;
74
+ }
75
+ void Update()
76
+ {
77
+ // ▼▼▼カメラの難しい処理▼▼▼
78
+ var cameraForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized; // カメラが追従するための動作
79
+ Vector3 direction = cameraForward * Input.GetAxis("Vertical") + Camera.main.transform.right * Input.GetAxis("Horizontal");
80
+
81
+
82
+ if (Input.GetAxis("Vertical") == 0 && Input.GetAxis("Horizontal") == 0) // テンキーや3Dスティックの入力(GetAxis)がゼロの時の動作
83
+ {
84
+ }
85
+ else // テンキーや3Dスティックの入力(GetAxis)がゼロではない時の動作
86
+ {
87
+ MukiWoKaeru(direction); // 向きを変える動作の処理を実行する(後述)
88
+ }
89
+
90
+ if (state == MyState.Normal)
91
+ {
92
+ // ▼▼▼落下処理▼▼▼
93
+ if (!isGroundCollider)
94
+ {
95
+ if (Physics.Linecast(charaRay.position, (charaRay.position - transform.up * charaRayRange)))
96
+ {
97
+ isGround = true;
98
+ rigid.useGravity = true;
99
+ }
100
+ else
101
+ {
102
+ isGround = false;
103
+ rigid.useGravity = false;
104
+ }
105
+ Debug.DrawLine(charaRay.position, (charaRay.position - transform.up * charaRayRange), Color.red);
106
+ }
107
+ // キャラクターコライダが接地、またはレイが地面に到達している場合
108
+ if (isGroundCollider || isGround)
109
+ {
110
+ // 地面に接地してる時は初期化
111
+ if (isGroundCollider)
112
+ {
113
+ rigid.useGravity = true;
114
+ moveDirection = Vector3.zero; //Y方向への速度をゼロにする
115
+ moveDirection = direction * idoSpeed; //移動スピードを向いている方向に与える
116
+ animCon.SetBool("Fall", false);
117
+ animCon.SetBool("Jump", false);
118
+ }
119
+ else
120
+ {
121
+ moveDirection = new Vector3(0f, moveDirection.y, 0f);
122
+ }
123
+
124
+ if (moveDirection.magnitude > 0f)
125
+ {
126
+ //着地後移動キーを押したら着地アニメーション終了
127
+ animCon.SetBool("Landing", false);
128
+ animCon.SetFloat("Speed", moveDirection.magnitude);
129
+ transform.LookAt(transform.position + moveDirection);
130
+ Debug.DrawLine(transform.position + new Vector3(0f, stepOffset, 0f), transform.position + new Vector3(0f, stepOffset, 0f) + transform.forward * slopeDistance, Color.green);
131
+ // ステップ用のレイが地面に接触しているかどうか
132
+ if (Physics.Linecast(stepRay.position, stepRay.position + stepRay.forward * stepDistance, out stepHit, LayerMask.GetMask("Field", "Block")))
133
+ {
134
+ // ステップアップ用のレイが地面と接触していない
135
+ if (!Physics.Linecast(transform.position + new Vector3(0f, stepOffset, 0f), transform.position + new Vector3(0f, stepOffset, 0f) + transform.forward * slopeDistance, LayerMask.GetMask("Field", "Block")))
136
+ {
137
+ moveDirection = Vector3.up * stepUpPower + transform.forward * idoSpeed;
138
+ // 接触した地面が登れる坂の角度以下
139
+ }
140
+ else if (Vector3.Angle(transform.up, stepHit.normal) <= slopeLimit)
141
+ {
142
+ moveDirection = Quaternion.FromToRotation(Vector3.up, stepHit.normal) * transform.forward * idoSpeed;
143
+ // それ以外は前進のみ
144
+ }
145
+ else
146
+ {
147
+ moveDirection += transform.forward * idoSpeed;
148
+ }
149
+
150
+ // ステップ用のレイが地面に接触していなければ
151
+ }
152
+ else
153
+ {
154
+ moveDirection += transform.forward * idoSpeed;
155
+ }
156
+ }
157
+ //キーの押しが小さすぎる場合は移動しない
158
+ else
159
+ {
160
+ animCon.SetFloat("Speed", 0f);
161
+ }
162
+ if (Input.GetKeyDown("space") || Input.GetButtonDown("Jump") && !animCon.GetCurrentAnimatorStateInfo(0).IsName("Jump")
163
+ && !animCon.IsInTransition(0))//Spaceキーorジャンプボタンが押されている場合
164
+ {
165
+ moveDirection.y += jumpPower; //Y方向への速度に「ジャンプパワー」の変数を代入する
166
+ animCon.SetBool("Jump", true);
167
+ rigid.useGravity = false;
168
+ }
169
+ /*else
170
+ {
171
+ // ジャンプキーを押していない時は下向きに力を加える
172
+ moveDirection += addForceDownPower;
173
+ }
174
+ }
175
+ else if (!animCon.GetBool("Fall"))
176
+ {
177
+ Debug.DrawLine(spine.position, spine.position + Vector3.down * distanceToTheGround, Color.red);
178
+ if (!Physics.SphereCast(new Ray(spine.position, Vector3.down), charaCon.radius, distanceToTheGround, LayerMask.GetMask("Field")))
179
+ {
180
+ animCon.SetBool("Fall", true);
181
+ }
182
+
183
+ }
184
+ else if (animCon.GetBool("Fall") || animCon.GetBool("Jump"))
185
+ {
186
+ Debug.DrawLine(spine.position, spine.position + Vector3.down * distanceToLanding, Color.green);
187
+ if (Physics.Linecast(spine.position, spine.position + Vector3.down * distanceToLanding, LayerMask.GetMask("Field")))
188
+ {
189
+ animCon.SetBool("Landing", true);
190
+ }
191
+ }
192
+
193
+ if (!isGroundCollider && !isGround) //CharacterControllerの付いているこのオブジェクトが接地していない場合の処理
194
+ {
195
+ rigid.useGravity = true;
196
+ moveDirection.y += Physics.gravity.y * Time.deltaTime;//マイナスのY方向(下向き)に重力を与える
197
+ if (Input.GetButtonDown("Fire1") && myStatus.GetWeaponStatus() != null
198
+ && myStatus.GetWeaponStatus().GetWeaponType() == WeaponStatus.WeaponType.Sword)
199
+ {
200
+ animCon.SetBool("JumpAttack", true);
201
+ }
202
+ else
203
+ {
204
+ animCon.SetBool("JumpAttack", false);
205
+ }
206
+ }
207
+
208
+ }
209
+ void FixedUpdate()
210
+ {
211
+ // キャラクターを移動させる処理
212
+ rigid.MovePosition(transform.position + moveDirection * Time.deltaTime);
213
+ }
214
+ void OnCollisionEnter()
215
+ {
216
+
217
+ Debug.DrawLine(charaRay.position, charaRay.position + Vector3.down, Color.blue);
218
+
219
+ // 他のコライダと接触している時は下向きにレイを飛ばしFieldかBlockレイヤーの時だけ接地とする
220
+ if (Physics.Linecast(charaRay.position, charaRay.position + Vector3.down, LayerMask.GetMask("Field", "Block")))
221
+ {
222
+ isGroundCollider = true;
223
+ }
224
+ else
225
+ {
226
+ isGroundCollider = false;
227
+ }
228
+ }
229
+
230
+ // 接触していなければ空中に浮いている状態
231
+ void OnCollisionExit()
232
+ {
233
+ isGroundCollider = false;
234
+ }
235
+
236
+ ![イメージ説明](49c8409d65f987e675d2d0e259d60790.png)
237
+ ![イメージ説明](32f6bb0e49ce4a20ec4a3551df2cd26f.png)
238
+ ![イメージ説明](9e0126a3ac1fa515c33a3009b9ce50f6.png)
239
+ ![イメージ説明](14e99175611ce21eb9da43e73be208bd.png)