Skip to content

Instantly share code, notes, and snippets.

@nabesi777
Last active September 25, 2018 13:21
Show Gist options
  • Select an option

  • Save nabesi777/04241a2543fcee453a0023edcf4080da to your computer and use it in GitHub Desktop.

Select an option

Save nabesi777/04241a2543fcee453a0023edcf4080da to your computer and use it in GitHub Desktop.
UnityChan ダンジョン向きController
using UnityEngine;
using System.Collections;
namespace UnityChan
{
// 必要なコンポーネントの列記
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(CapsuleCollider))]
[RequireComponent(typeof(Rigidbody))]
public class UniCon : MonoBehaviour
{
bool ground;
public float animSpeed = 1.5f; // アニメーション再生速度設定
public float lookSmoother = 3.0f; // a smoothing setting for camera motion
// 以下キャラクターコントローラ用パラメタ
// 前進速度
public float forwardSpeed = 2.0f;
// 後退速度
public float backwardSpeed = 1.0f;
// 旋回速度
public float rotateSpeed = 3.0f;
// ジャンプ威力
public float jumpPower = 4.0f;
// キャラクターコントローラ(カプセルコライダ)の参照
private CapsuleCollider col;
private Rigidbody rb;
// キャラクターコントローラ(カプセルコライダ)の移動量
private Vector3 velocity;
// CapsuleColliderで設定されているコライダのHeiht、Centerの初期値を収める変数
private float orgColHight;
private Vector3 orgVectColCenter;
private Animator anim; // キャラにアタッチされるアニメーターへの参照
private GameObject cameraObject; // メインカメラへの参照
// 初期化
void Start()
{
// Animatorコンポーネントを取得する
anim = GetComponent<Animator>();
// CapsuleColliderコンポーネントを取得する(カプセル型コリジョン)
col = GetComponent<CapsuleCollider>();
rb = GetComponent<Rigidbody>();
//メインカメラを取得する
cameraObject = GameObject.FindWithTag("MainCamera");
// CapsuleColliderコンポーネントのHeight、Centerの初期値を保存する
orgColHight = col.height;
orgVectColCenter = col.center;
}
// 以下、メイン処理.リジッドボディと絡めるので、FixedUpdate内で処理を行う.
void FixedUpdate()
{
if (ground==true)
{
anim.SetBool("Jump", false);
float h = Input.GetAxis("Horizontal"); // 入力デバイスの水平軸をhで定義
float v = Input.GetAxis("Vertical"); // 入力デバイスの垂直軸をvで定義
anim.SetFloat("Speed", v); // Animator側で設定している"Speed"パラメタにvを渡す
anim.SetFloat("Direction", h); // Animator側で設定している"Direction"パラメタにhを渡す
anim.speed = animSpeed; // Animatorのモーション再生速度に animSpeedを設定する
rb.useGravity = true;//ジャンプ中に重力を切るので、それ以外は重力の影響を受けるようにする
// 以下、キャラクターの移動処理
velocity = new Vector3(0, 0, v); // 上下のキー入力からZ軸方向の移動量を取得
// キャラクターのローカル空間での方向に変換
velocity = transform.TransformDirection(velocity);
//以下のvの閾値は、Mecanim側のトランジションと一緒に調整する
if (Input.GetKey(KeyCode.W))
{
velocity *= forwardSpeed; // 移動速度を掛ける *加速
}
else if (Input.GetKey(KeyCode.S))
{
velocity *= backwardSpeed; // 移動速度を掛ける
}
if (Input.GetKeyDown(KeyCode.Space) && ground == true)//spaceを押すと
{
velocity= new Vector3(0, jumpPower, 0);
anim.SetBool("Jump", true);
}
//groundがfalseの時
else
{
anim.SetBool("Jump", false);
ground = false;
}
// キー入力でキャラクターを移動させる
transform.localPosition += velocity * Time.fixedDeltaTime;
// キー入力でキャラクタをY軸で旋回させる
transform.Rotate(0, h * rotateSpeed, 0);
if (Input.GetKey(KeyCode.W))
{
anim.SetBool("Walk", true);
}
else
{
anim.SetBool("Walk", false);
}
if (Input.GetKey(KeyCode.S))
{
anim.SetBool("WalkBack", true);
}
else
{
anim.SetBool("WalkBack", false);
}
//shiftとwが押されている時
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W))
{//speedが3.5fになり、アニメーション"run"がtrue
forwardSpeed = 4f;
anim.SetBool("Run", true);
}
else
{
forwardSpeed = 2f;
anim.SetBool("Run", false);
}
//エモート
if (Input.GetKeyDown("1"))
{//1を押すとanimation””を呼び出し,最初のレイヤー、animation開始(0~1で指定)
anim.Play("WAIT01", -1, 0.0f);
}
if (Input.GetKeyDown("2"))
{
anim.Play("WAIT02", -1, 0.0f);
}
if (Input.GetKeyDown("3"))
{
anim.Play("WAIT03", -1, 0.0f);
}
if (Input.GetKeyDown("4"))
{
anim.Play("WAIT04", -1, 0.0f);
}
if (Input.GetKeyDown("5"))
{
anim.Play("WIN00", -1, 0.0f);
}
if (Input.GetKeyDown(KeyCode.F))
{
anim.Play("SLIDE00", -1, 0.0f);
}
}
}
private void OnCollisionStay(Collision collision)
{
if (collision.gameObject.tag == "Ground")//tag"Ground"に触れている状態で
{
ground = true;
}
else
{
ground = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment