Skip to content

Instantly share code, notes, and snippets.

@IshidaGames
Created October 25, 2017 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IshidaGames/54046205229bf3ab991bf39ce1e74408 to your computer and use it in GitHub Desktop.
Save IshidaGames/54046205229bf3ab991bf39ce1e74408 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour
{
Rigidbody rb;
//移動スピード
public float speed = 2f;
//ジャンプ力
public float thrust = 100;
//Animatorを入れる変数
private Animator animator;
//Planeに触れているか判定するため
bool ground;
void Start()
{
rb = GetComponent<Rigidbody>();
//UnityちゃんのAnimatorにアクセスする
animator = GetComponent<Animator>();
}
void Update()
{
//地面に触れている場合発動
if (ground)
{
//上下左右のキーでの移動、向き、アニメーション
if (Input.GetKey(KeyCode.RightArrow))
{
//移動(X軸、Y軸、Z軸)
rb.velocity = new Vector3(speed, 0, 0);
//向き(X軸、Y軸、Z軸)
transform.rotation = Quaternion.Euler(0, 90, 0);
//アニメーション
animator.SetBool("Running", true);
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
rb.velocity = new Vector3(-speed, 0, 0);
transform.rotation = Quaternion.Euler(0, 270, 0);
animator.SetBool("Running", true);
}
else if (Input.GetKey(KeyCode.UpArrow))
{
rb.velocity = new Vector3(0, 0, speed);
transform.rotation = Quaternion.Euler(0, 0, 0);
animator.SetBool("Running", true);
}
else if (Input.GetKey(KeyCode.DownArrow))
{
rb.velocity = new Vector3(0, 0, -speed);
transform.rotation = Quaternion.Euler(0, 180, 0);
animator.SetBool("Running", true);
}
//何もキーを押していない時はアニメーションをオフにする
else
{
animator.SetBool("Running", false);
}
//スペースキーでジャンプする
if (Input.GetKey(KeyCode.Space))
{
animator.SetBool("Jumping", true);
//上方向に向けて力を加える
rb.AddForce(new Vector3(0, thrust, 0));
ground = false;
}
else
{
animator.SetBool("Jumping", false);
}
}
}
//別のCollider、今回はPlaneに触れているかどうかを判断する
void OnCollisionStay(Collision col)
{
ground = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment