Skip to content

Instantly share code, notes, and snippets.

@IshidaGames
Created November 23, 2019 12:13
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/3513f1fd76626efa088beab9d348a0db to your computer and use it in GitHub Desktop.
Save IshidaGames/3513f1fd76626efa088beab9d348a0db to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CapsuleCollider))]
[RequireComponent(typeof(Rigidbody))]
public class Player : MonoBehaviour
{
[SerializeField] float speed = 4f;
private Animator animator;
Transform cam;
Rigidbody rb;
CapsuleCollider caps;
enum Action
{
Move,
Attack,
Damage
}
[SerializeField] Action action = Action.Move;
[SerializeField]GameObject attackObj;
//SEオブジェクトを入れる
GameObject seObj;
//SEスクリプトを入れる
SE seScr;
void Start()
{
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody>();
rb.constraints = RigidbodyConstraints.FreezeRotation;
cam = Camera.main.transform;
caps = GetComponent<CapsuleCollider>();
caps.center = new Vector3(0, 0.76f, 0);
caps.radius = 0.23f;
caps.height = 1.6f;
attackObj.SetActive(false);
//SEオブジェクトを取得
seObj = GameObject.Find("SE");
//SEスクリプトを取得
seScr = seObj.GetComponent<SE>();
}
void Update()
{
if (action == Action.Move)
{
float x = Input.GetAxisRaw("Horizontal") * Time.deltaTime * speed;
float z = Input.GetAxisRaw("Vertical") * Time.deltaTime * speed;
animator.SetFloat("X", x * 50);
animator.SetFloat("Y", z * 50);
if (z > 0)
{
transform.rotation = Quaternion.Euler(new Vector3(transform.rotation.x,
cam.eulerAngles.y, transform.rotation.z));
}
else if (z < 0)
{
z = z / 1.5f;
}
transform.position += transform.forward * z + transform.right * x;
if (Input.GetMouseButtonDown(0))
{
action = Action.Attack;
//SEスクリプトのAttackSound()を呼ぶ
seScr.AttackSound();
}
}
else if (action == Action.Attack)
{
animator.SetBool("Attack", true);
attackObj.SetActive(true);
}
else if (action == Action.Damage)
{
animator.SetBool("Damage", true);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "EnemyAttack")
{
if (action == Action.Damage)
{
return;
}
else
{
action = Action.Damage;
//SEスクリプトのDamageSound()を呼ぶ
seScr.DamageSound();
}
}
}
public void AttackEnd()
{
Invoke("AttackEndAnim", 0.5f);
}
void AttackEndAnim()
{
action = Action.Move;
animator.SetBool("Attack", false);
attackObj.SetActive(false);
}
void DamageEnd()
{
action = Action.Move;
animator.SetBool("Damage", false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment