Created
September 26, 2018 09:05
-
-
Save IshidaGames/6a952762ccfca881959f6606e9582c13 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; | |
public class Player : MonoBehaviour { | |
//Rigidbodyを変数に入れる | |
Rigidbody rb; | |
//移動スピード | |
public float speed = 3f; | |
//ジャンプ力 | |
public float thrust = 200; | |
//Animatorを入れる変数 | |
private Animator animator; | |
//ユニティちゃんの位置を入れる | |
Vector3 playerPos; | |
//地面に接触しているか否か | |
bool ground; | |
void Start() | |
{ | |
//Rigidbodyを取得 | |
rb = GetComponent<Rigidbody>(); | |
//ユニティちゃんのAnimatorにアクセスする | |
animator = GetComponent<Animator>(); | |
//ユニティちゃんの現在より少し前の位置を保存 | |
playerPos = transform.position; | |
} | |
void Update() | |
{ | |
//地面に接触していると作動する | |
if (ground) | |
{ | |
//A・Dキー、←→キーで横移動 | |
float x = Input.GetAxisRaw("Horizontal") * Time.deltaTime * speed; | |
//W・Sキー、↑↓キーで前後移動 | |
float z = Input.GetAxisRaw("Vertical") * Time.deltaTime * speed; | |
//現在の位置+入力した数値の場所に移動する | |
rb.MovePosition(transform.position + new Vector3(x, 0, z)); | |
//ユニティちゃんの最新の位置から少し前の位置を引いて方向を割り出す | |
Vector3 direction = transform.position - playerPos; | |
//移動距離が少しでもあった場合に方向転換 | |
if (direction.magnitude > 0.01f) | |
{ | |
//directionの方向を向かせる | |
transform.rotation = Quaternion.LookRotation(direction); | |
//走るアニメーションを再生 | |
animator.SetBool("Running", true); | |
} | |
else | |
{ | |
//ベクトルの長さがない=移動していない時は走るアニメーションはオフ | |
animator.SetBool("Running", false); | |
} | |
//ユニティちゃんの位置を更新する | |
playerPos = transform.position; | |
//スペースキーやゲームパッドの3ボタンでジャンプ | |
if (Input.GetButton("Jump")) | |
{ | |
//thrustの分だけ上方に力がかかる | |
rb.AddForce(transform.up * thrust); | |
//速度が出ていたら前方と上方に力がかかる | |
if (rb.velocity.magnitude > 0) | |
rb.AddForce(transform.forward * thrust + transform.up * thrust); | |
} | |
} | |
} | |
//Planに触れている間作動 | |
void OnCollisionStay(Collision col) | |
{ | |
ground = true; | |
//ジャンプのアニメーションをオフにする | |
animator.SetBool("Jumping", false); | |
} | |
//Planから離れると作動 | |
void OnCollisionExit(Collision col) | |
{ | |
ground = false; | |
//ジャンプのアニメーションをオンにする | |
animator.SetBool("Jumping", true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment