Skip to content

Instantly share code, notes, and snippets.

@nabesi777
Created September 30, 2018 22:42
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 nabesi777/8bbbfcb9988a071f7c7f9619ecec2cdb to your computer and use it in GitHub Desktop.
Save nabesi777/8bbbfcb9988a071f7c7f9619ecec2cdb to your computer and use it in GitHub Desktop.
キャラ動き(階段・ジャンプ・走り・歩き)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class ChaCon : MonoBehaviour
{
public float speed;
public float rotateSpeed = 3.0F;
public float gravity = 10f;
public float jumpPower = 5;
public float animSpeed = 2f;
private Animator anim;
private Vector3 moveDirection;
private CharacterController controller;
void Start()
{
// コンポーネントの取得
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
void Update()
{
if (controller.isGrounded)
{
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);
}
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W))
{//speedが3.5fになり、アニメーション"run"がtrue
speed = 6f;
anim.SetBool("Run", true);
}
else
{
speed = 3f;
anim.SetBool("Run", false);
}
// ジャンプ
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpPower;
anim.SetBool("Jump", true);
}
else
{
// 重力
moveDirection.y -= gravity * Time.deltaTime;
anim.SetBool("Jump", false);
}
// キャラクターのローカル空間での方向
moveDirection = transform.transform.forward * speed * Input.GetAxis("Vertical");
// 回転
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
// SimpleMove関数で移動させる
controller.Move(moveDirection * Time.deltaTime);
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);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment