구글링도하고 유튜브도 봐보고 했는데 점프가 구현이 안되더라고
일단은 애니메이터는 이렇게 되어있고
소스코드는
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyController : MonoBehaviour
{
public enum State
{
Idle,
Move,
Jump,
Skill,
Slide,
}
float horizontalMove;
float verticalMove;
[SerializeField]
private float _jumpPower = 5f;
[SerializeField]
private float _speed = 0f;
float move_speed = 0;
Rigidbody _rigidbody;
Animator _animator;
bool _isJumpping;
Vector3 _movement;
State _state = State.Idle;
void Awake()
{
_rigidbody = GetComponent<Rigidbody>();
_animator = GetComponent<Animator>();
}
void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal"); // 왼, 오
verticalMove = Input.GetAxisRaw("Vertical");
if (horizontalMove == 0 && verticalMove == 0)
{
move_speed = Mathf.Lerp(move_speed, 0, 10.0f * Time.deltaTime);
_animator.SetFloat("Horizontal", move_speed);
_animator.SetFloat("Vertical", move_speed);
_animator.Play("MOVE_2D_Direction");
}
else
{
Move();
}
if (Input.GetButtonDown("Jump"))
{
Jump();
}
if (Input.GetKey(KeyCode.Z))
{
Skill();
}
if (Input.GetKey(KeyCode.LeftControl))
{
Slide();
}
}
void Move()
{
if (horizontalMove == 0 && verticalMove == 0)
{
_state = State.Idle;
return;
}
_movement.Set(horizontalMove, 0, verticalMove);
if (Input.GetKey(KeyCode.LeftShift))
{
_speed = 10f;
move_speed = Mathf.Lerp(move_speed, 1f, 10.0f * Time.deltaTime);
_movement = _movement.normalized * _speed * Time.deltaTime;
}
else
{
_speed = 5f;
move_speed = Mathf.Lerp(move_speed, 0.5f, 10.0f * Time.deltaTime);
_movement = _movement.normalized * _speed * Time.deltaTime;
}
_rigidbody.MovePosition(transform.position + _movement);
Quaternion quaternion = Quaternion.LookRotation(_movement);
_rigidbody.rotation = Quaternion.Slerp(_rigidbody.rotation,quaternion,0.2f);
_animator.SetFloat("Horizontal", move_speed);
_animator.SetFloat("Vertical", move_speed);
_animator.Play("MOVE_2D_Direction");
}
void Jump()
{
if (!_isJumpping)
{
_rigidbody.AddForce(Vector3.up * _jumpPower, ForceMode.Impulse);
_animator.SetBool("IsJumpping", true);
_animator.CrossFade("JUMP", 0.1f, -1, 0);
_isJumpping = true;
}
}
void Skill()
{
_animator.CrossFade("ATTACK", 0.1f, -1, 0);
}
void Slide()
{
_animator.CrossFade("SLIDE", 0.1f, -1, 0);
}
void OnHitEvent()
{
Debug.Log("Attack!");
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Floor")
{
_animator.SetBool("IsJumpping", false);
_animator.SetBool("IsJumpping", false);
_isJumpping = false;
}
}
}
애니메이터가 짤려서 글로쓸게
MOVE_2D_Direction 블렌드트리가 중앙에 있고 transition으로 bool만들어서 각각에 condition에 넣어놨고 jump, attack , slide 애니메이션이랑 서로 이어놨어
점프할때 OnCollisionEnter가 점프메소드보다 나중에 실행 된것 같은데
Animator 봐야 알겠는데 FSM에서 값이 변하는 거까지 확인된 거지?