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

質問編集履歴

1

Player.csを追加

2018/05/29 14:57

投稿

mikaduki444
mikaduki444

スコア6

title CHANGED
File without changes
body CHANGED
@@ -45,7 +45,106 @@
45
45
  }
46
46
 
47
47
  ```
48
+ ### Player.cs
49
+ ```C#
50
+ using System.Collections;
51
+ using System.Collections.Generic;
52
+ using UnityEngine;
48
53
 
54
+ public class Player: MonoBehaviour
55
+ {
56
+ [SerializeField] Vector3 velocity; //移動方向
57
+ public static float speed = 2.0f; //移動速度
58
+ [SerializeField] float rotation = 0.2f; //回転速度
59
+ public static Animator animator;
60
+
61
+ void Start()
62
+ {
63
+ animator = GetComponent<Animator>();
64
+ }
65
+
66
+ void Update()
67
+ {
68
+ velocity = Vector3.zero;
69
+
70
+ if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.UpArrow))
71
+ {
72
+ animator.SetBool("Walking", true);
73
+ if (!ForestSound.footsteps.isPlaying) { ForestSound.footsteps.PlayOneShot(ForestSound.footsteps.clip); }
74
+
75
+ if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
76
+ {
77
+ speed = 5.0f;
78
+ if (!ForestSound.footsteps.isPlaying) { ForestSound.footsteps.PlayOneShot(ForestSound.footsteps.clip); }
79
+ animator.SetBool("Walking", false);
80
+ animator.SetBool("Running", true);
81
+ }
82
+ if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))
83
+ {
84
+ speed = 2.0f;
85
+ if (ForestSound.footsteps.isPlaying) { ForestSound.footsteps.Stop(); }
86
+ animator.SetBool("Running", false);
87
+ animator.SetBool("Walking", true);
88
+ }
89
+ }
90
+ else
91
+ {
92
+ animator.SetBool("Walking", false);
93
+ animator.SetBool("Running", false);
94
+ if (ForestSound.footsteps.isPlaying) { ForestSound.footsteps.Stop(); }
95
+ }
96
+
97
+ if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
98
+ {
99
+ //移動(X軸、Y軸、Z軸)
100
+ velocity.x += 1;
101
+ }
102
+ if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
103
+ {
104
+ velocity.x -= 1;
105
+ }
106
+ if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
107
+ {
108
+ velocity.z += 1;
109
+ }
110
+ if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
111
+ {
112
+ velocity.z -= 1;
113
+ }
114
+ if ((Input.GetKey(KeyCode.W)&& Input.GetKey(KeyCode.A)) || (Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.LeftArrow)))
115
+ {
116
+ velocity.x -= 1;
117
+ velocity.z += 1;
118
+ }
119
+ if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D) || (Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.RightArrow)))
120
+ {
121
+ velocity.x += 1;
122
+ velocity.z += 1;
123
+ }
124
+ if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A) || (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.LeftArrow)))
125
+ {
126
+ velocity.x -= 1;
127
+ velocity.z -= 1;
128
+ }
129
+ if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D) || (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.RightArrow)))
130
+ {
131
+ velocity.x += 1;
132
+ velocity.z -= 1;
133
+ }
134
+
135
+ velocity = velocity.normalized * speed * Time.deltaTime;
136
+ if(velocity.magnitude > 0)
137
+ {
138
+ transform.position += velocity;
139
+ transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(velocity), rotation);
140
+ }
141
+
142
+ }
143
+
144
+ }
145
+
146
+ ```
147
+
49
148
  ### 試したこと
50
149
 
51
150
  AddForceなども試しましたが挙動が怪しく、velocityで実装したい所存です。