Unity/수업 내용
[2D]프로젝트Play&plane -키보드 컨트롤 (애니메이션)
이지훈26
2021. 10. 13. 10:56
*꼬리 흔들기(애니메이션)
window -> animation -> animation을 만들고 create를 누른다
Animation 폴더를 만들고 그 안에 이름을 Center로 만들어 넣는다
Scriptes 안에 Player를 보면 Center0~3, Left0~3, Right0~3이 있는데 Center0~3을 애니메이션에 넣어주고 마지막에 AddKeyFrame을 눌러 끝을 찍어준다
Center를 누르고 CreatNewClip을 눌러 Left와 Right도 똑같이 만들어 준다
*좌,우 키보드 컨트롤
*여기서 true와 false를 잘 넣어야 한다 2시간 만에 해결.
코드
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private bool isPressLeftArrow = false;
private bool isPressRightArrow = false;
private int prevKey = 0; // -1 : left, 1 : right
private Animator animator;
// Start is called before the first frame update
void Start()
{
this.animator = this.GetComponent<Animator>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
this.isPressLeftArrow = true;
if (this.isPressRightArrow)
{
prevKey = 1;
this.isPressRightArrow = false;
}
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
this.isPressRightArrow = true;
if (this.isPressLeftArrow)
{
prevKey = -1;
this.isPressLeftArrow = false;
}
}
if (Input.GetKeyUp(KeyCode.LeftArrow))
{
this.isPressLeftArrow = false;
if (this.prevKey == 1)
{
this.isPressRightArrow = true;
this.prevKey = 0;
}
}
if (Input.GetKeyUp(KeyCode.RightArrow))
{
this.isPressRightArrow = false;
if (this.prevKey == -1)
{
this.isPressLeftArrow = true;
this.prevKey = 0;
}
}
this.animator.SetBool("IsPressLeftArrow", this.isPressLeftArrow);
this.animator.SetBool("IsPressRightArrow", this.isPressRightArrow);
}
}