Created
June 6, 2019 12:16
-
-
Save IshidaGames/5cd5b8d1b6b7f54eefba45a49d5dc431 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
//CharacterControllerを入れる | |
[RequireComponent(typeof(CharacterController))] | |
public class FirstPerson : MonoBehaviour | |
{ | |
private float speed; | |
//落ちる速さ、重力 | |
public float gravity = 10f; | |
private float jumpSpeed; | |
//通常時のスピードとジャンプ力 | |
public float normalSpeed = 5; | |
public float normalJump = 10; | |
//左Shift押したときのスピードとジャンプ力 | |
public float shiftSpeed = 10; | |
public float shiftJump = 20; | |
//Playerの移動や向く方向を入れる | |
Vector3 moveDirection; | |
//CharacterControllerを変数にする | |
CharacterController controller; | |
//マウスの横縦に動かす速さ | |
public float horizontalSpeed = 2.0F; | |
public float verticalSpeed = 2.0F; | |
//Main Cameraを入れる | |
GameObject came; | |
void Start() | |
{ | |
//CharacterControllerを取得 | |
controller = GetComponent<CharacterController>(); | |
//Main Cameraを検索し子オブジェクトにしてPlayerに設置する | |
came = Camera.main.gameObject; | |
came.transform.parent = this.transform; | |
//カメラを目線の高さにする | |
came.transform.localPosition = new Vector3(0, 0.4f, 0); | |
//カメラの向きをこのオブジェクトと同じにする | |
came.transform.rotation = this.transform.rotation; | |
} | |
void Update() | |
{ | |
//左右どちらかのShift押した場合と離している場合 | |
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) | |
{ | |
speed = shiftSpeed; | |
jumpSpeed = shiftJump; | |
} | |
else | |
{ | |
speed = normalSpeed; | |
jumpSpeed = normalJump; | |
} | |
//マウスでカメラの向きとPlayerの横の向きを変える | |
float h = horizontalSpeed * Input.GetAxis("Mouse X"); | |
float v = verticalSpeed * Input.GetAxis("Mouse Y"); | |
transform.Rotate(0, h, 0); | |
came.transform.Rotate(v, 0, 0); | |
//Playerが地面に設置していることを判定 | |
if (controller.isGrounded) | |
{ | |
//XZ軸の移動と向きを代入する | |
//WASD,上下左右キー | |
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, | |
Input.GetAxis("Vertical")); | |
moveDirection = transform.TransformDirection(moveDirection); | |
moveDirection *= speed; | |
// Y軸方向にジャンプさせる | |
if (Input.GetButtonDown("Jump")) | |
moveDirection.y = jumpSpeed; | |
} | |
// 重力を設定しないと落下しない | |
moveDirection.y -= gravity * Time.deltaTime; | |
// Move関数に代入する | |
controller.Move(moveDirection * Time.deltaTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment