728x90
<제 2강 캐릭터 애니메이션>
Has Exit Time(모션대기) 설정을 풀어준다 ->부드럽게 움직이게 하기 위해서
private Animator animator;
void Start()
{
animator = GetComponent<Animator>(); // animator 변수로 제어 가능
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingObject : MonoBehaviour
{
public float speed;
private Vector3 vector;
public float runSpeed;
private float applyRunSpeed;
private bool applyRunFlag = false;
public int walkCount;
private int currentWalkCount;
private bool canMove = true;
private Animator animator;
//speed=2.4, walkCount=20
//2.4*20=48
void Start()
{
animator = GetComponent<Animator>();
}
IEnumerator MoveCoroutine()
{
if (Input.GetKey(KeyCode.LeftShift))
{
applyRunSpeed = runSpeed;
applyRunFlag = true;
}
else
{
applyRunSpeed = 0;
applyRunFlag = false;
}
vector.Set(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), transform.position.z);
animator.SetFloat("DirX", vector.x);
animator.SetFloat("DirY",vector.y);
animator.SetBool("Walking", true);
while (currentWalkCount < walkCount)
{
if (vector.x != 0)
{
transform.Translate(vector.x * (speed + applyRunSpeed), 0, 0);
}
else if (vector.y != 0)
{
transform.Translate(0, vector.y * (speed + applyRunSpeed), 0);
}
if (applyRunFlag)
{
currentWalkCount++;
}
currentWalkCount++;
yield return new WaitForSeconds(0.01f);
}//실제 이동
currentWalkCount = 0;
animator.SetBool("Walking", false);
canMove = true;
}
// Update is called once per frame
void Update()
{
if (canMove)
{
if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
{
canMove = false;
StartCoroutine(MoveCoroutine());
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingObject : MonoBehaviour
{
public float speed;
private Vector3 vector;
public float runSpeed;
private float applyRunSpeed;
private bool applyRunFlag = false;
public int walkCount;
private int currentWalkCount;
private bool canMove = true;
private Animator animator;
//speed=2.4, walkCount=20
//2.4*20=48
void Start()
{
animator = GetComponent<Animator>();
}
IEnumerator MoveCoroutine()
{
while (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)//한발씩 움직이게 함
{
if (Input.GetKey(KeyCode.LeftShift))
{
applyRunSpeed = runSpeed;
applyRunFlag = true;
}
else
{
applyRunSpeed = 0;
applyRunFlag = false;
}
vector.Set(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), transform.position.z);
if (vector.x != 0)
{
vector.y = 0;
}//vector.x=1 우측이동
//vector.y=0
animator.SetFloat("DirX", vector.x);
animator.SetFloat("DirY", vector.y);
animator.SetBool("Walking", true);
while (currentWalkCount < walkCount)
{
if (vector.x != 0)
{
transform.Translate(vector.x * (speed + applyRunSpeed), 0, 0);
}
else if (vector.y != 0)
{
transform.Translate(0, vector.y * (speed + applyRunSpeed), 0);
}
if (applyRunFlag)
{
currentWalkCount++;
}
currentWalkCount++;
yield return new WaitForSeconds(0.01f);
}//실제 이동
currentWalkCount = 0;
}
animator.SetBool("Walking", false);
canMove = true;
}
void Update()
{
if (canMove)
{
if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
{
canMove = false;
StartCoroutine(MoveCoroutine());
}
}
}
}
코루틴이 한번 실행된뒤 다시 입력되게 설정하면 한발씩 움직이는 것처럼 보임(한번 방향키 입력받고 코루틴이 실행된 후 또 방향키 입력받게 설정되어 있기 때문에)
->이를 수정하기위해 코루틴에 반복문 추가
728x90
'🎮 취미 > Unity' 카테고리의 다른 글
유니티 스터디 - 게임 개발 속성 강의 (2) (2) | 2024.11.15 |
---|---|
유니티 스터디 - 게임 개발 속성 강의 (1) (1) | 2024.11.08 |
유니티 쯔꾸르 게임 만들기 4 (인프런) (0) | 2023.07.01 |
유니티 쯔꾸르 게임 만들기 3 (인프런) (0) | 2023.07.01 |
유니티 쯔꾸르 게임 만들기 1 (인프런) (0) | 2023.06.30 |