プレーヤーの動きを改良する
![999bce38 e7fb 4771 8675 95a9f344bbad](https://codegenius.org/uploads/slide/image/2467/999bce38-e7fb-4771-8675-95a9f344bbad.jpeg)
![29aae6a6 3b27 4a15 b1cb 4c38475a6269](https://codegenius.org/uploads/slide/image/2468/29aae6a6-3b27-4a15-b1cb-4c38475a6269.jpeg)
プレーヤーの動きの改良
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
private CharacterController controller;
private Vector3 moveDirection = Vector3.zero;
public float gravity = 9.8f;
public float speedZ;
// ★追加(横移動のスピード)
public float speedX;
// ★追加(旋回のスピード)
public float turnSpeed;
void Start(){
controller = GetComponent<CharacterController> ();
}
void Update(){
if (controller.isGrounded) {
moveDirection.z = Input.GetAxis ("Vertical") * Time.deltaTime * speedZ;
// (★)下記のコードをコメントアウトしましょう。
//transform.Rotate (0, Input.GetAxis ("Horizontal") * 2, 0);
// ★追加
// 左右キーで水平横移動
moveDirection.x = Input.GetAxis("Horizontal") * Time.deltaTime * speedX;
// ★追加
// マウスの左右移動で旋回
transform.Rotate(0, Input.GetAxis("Mouse X") * turnSpeed, 0);
}
moveDirection.y -= gravity * Time.deltaTime;
Vector3 globalDirection = transform.TransformDirection (moveDirection);
controller.Move (globalDirection);
}
}
![Ce3ba3cd 3a67 40bc 8f02 30737f8162fa](https://codegenius.org/uploads/slide/image/2469/ce3ba3cd-3a67-40bc-8f02-30737f8162fa.jpeg)
![3ba65d6c b8c8 4085 b073 0dce68219f64](https://codegenius.org/uploads/slide/image/2470/3ba65d6c-b8c8-4085-b073-0dce68219f64.jpeg)
EscapeCombat(メモ)
他のコースを見る![999bce38 e7fb 4771 8675 95a9f344bbad](https://codegenius.org/uploads/slide/image/2467/999bce38-e7fb-4771-8675-95a9f344bbad.jpeg)
![29aae6a6 3b27 4a15 b1cb 4c38475a6269](https://codegenius.org/uploads/slide/image/2468/29aae6a6-3b27-4a15-b1cb-4c38475a6269.jpeg)
プレーヤーの動きの改良
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
private CharacterController controller;
private Vector3 moveDirection = Vector3.zero;
public float gravity = 9.8f;
public float speedZ;
// ★追加(横移動のスピード)
public float speedX;
// ★追加(旋回のスピード)
public float turnSpeed;
void Start(){
controller = GetComponent<CharacterController> ();
}
void Update(){
if (controller.isGrounded) {
moveDirection.z = Input.GetAxis ("Vertical") * Time.deltaTime * speedZ;
// (★)下記のコードをコメントアウトしましょう。
//transform.Rotate (0, Input.GetAxis ("Horizontal") * 2, 0);
// ★追加
// 左右キーで水平横移動
moveDirection.x = Input.GetAxis("Horizontal") * Time.deltaTime * speedX;
// ★追加
// マウスの左右移動で旋回
transform.Rotate(0, Input.GetAxis("Mouse X") * turnSpeed, 0);
}
moveDirection.y -= gravity * Time.deltaTime;
Vector3 globalDirection = transform.TransformDirection (moveDirection);
controller.Move (globalDirection);
}
}
![Ce3ba3cd 3a67 40bc 8f02 30737f8162fa](https://codegenius.org/uploads/slide/image/2469/ce3ba3cd-3a67-40bc-8f02-30737f8162fa.jpeg)
![3ba65d6c b8c8 4085 b073 0dce68219f64](https://codegenius.org/uploads/slide/image/2470/3ba65d6c-b8c8-4085-b073-0dce68219f64.jpeg)
プレーヤーの動きを改良する