์ผ์ด๋ ์ฏ๊พธ๋ฅด ๊ฐ์ ๋ฃ๊ณ ์๋๋ฐ
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
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 void Start()
{
}
IEnumerator MoveCorutine()
{
vector.Set(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), transform.position.z);
if (Input.GetKey(KeyCode.LeftShift))
{
applyRunSpeed = runSpeed;
}
else
{
applyRunSpeed = 0;
applyRunFlag = 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;
canMove = true;
}
private void Update()
{
if (canMove)
{
if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
canMove = false;
StartCoroutine(MoveCorutine());
}
}
}
}
์ด์ฝ๋์์ ๊ฒฐ๊ตญ์๋ leftshift๋ฅผ ๋๋ฅด๋ฉด while (currentWalkCount < walkCount)์ด๊ฒ 10๋ฒ ๋๋ฅด์ง ์์ผ๋ฉด 20๋ฒ ์คํ๋๋๋ฐ speed๊ฐ๊ณ applyRunSpeed ๊ฐ์ด 2.4๋ก ๋์ผํ๋ leftshift๋ฅผ ๋๋ ์๋๋ ์๋๋ ์๋๋ 48๋ก ์๋๊ฐ ๋๊ฐ์ง ์๋์? ๊ทธ๋ฐ๋ฐ ์ ์ ๋ํฐ ์์์๋ leftshift๋ฅผ ๋๋ฅธ๊ฒ ๋ ๋น ๋ฅด๊ฒ ๋์ฌ๊น์?
speed์ applyRunSpeed๋ฅผ ๋ํ๋ฉด 4.8์ด์์ ๊ทธ๋ฌ๋ฉด 2๋ฐฐ๋ก ๋นจ๋ผ์ง์ง - dc App
์ฝ๋ฃจํด ์คํ ์์ ์์ leftshift๋ฅผ ๋๋ฅด๊ณ ์์๋ค๋ฉด walkCount์ ๋๋ฌํ ๋๊น์ง currentWalkCount๋ฅผ ํ๋ฒ ๋ํ๊ณ speed ์ runSpeed๋ฅผ ๋ํ๊ฐ๋งํผ ์ด๋ ์ฝ๋ฃจํด ์คํ ์์ ์์ leftshift๋ฅผ ๋๋ฅด๊ณ ์์ง ์์๋ค๋ฉด walkCount์ ๋๋ฌํ ๋๊น์ง currentWalkCount ๋๋ฒ ๋ํ๊ณ speed๊ฐ ๋งํผ ์ด๋ speed ์ runSpeed๊ฐ ๋์ผํ ๊ฐ์ด๋ผ๋ฉด ์๋ 4๋ฐฐ ์ฐจ์ด ๋๋๊ฑฐ ์๋๊ฐ? ๊ทผ๋ฐ ์ ์์ ์ธ ์ฝ๋ ๋ง์? currentWalkCount๋ฅผ ๋๋ฒ์ฉ ๋ํ๋ ๊ตฌ์กฐ๋ก ์์ฑ๋ ์ด์ ๋ฅผ ๋ชจ๋ฅด๊ฒ ๋ค
left๋ฅผ ์ ๋๋ฅด๋ฉด applyRunflag๊ฐ true๊ฐ ๋๋ ํฉ๊ณ 20, left๋ฅผ ๋๋ฅด๋ฉด applyRunflag false๋๋ ํฉ๊ณ 10์ด๋ผ์ ๊ทธ๋ฐ๊ฑฐ ์๋? ๊ทธ๋ฌ๋ฉด ๋๋ฐฐ ๋นจ๋ผ์ง๋๊ฑฐ ๊ฐ์๋ฐ - dc App - dc App
ํ์ผ ํฌ๊ธฐ๊ฐ 48ํฝ์ ์ด๋ผ๊ณ ๊ฐ์ ํ๋ฉด, 4.8 x 10๋ฒ์ผ๋ก ์์ง์ด๊ธฐ or 2.4๋ก 20๋ฒ ์์ง์ด๊ธฐ ๋น์ฐํ ์ ์๊ฐ ๋ ๋น ๋ฅด์ง - dc App
๊ฐ์ฌํฉ๋๋ค~
์ฒซ ์ฝ๋ฃจํด ์์์๋ currentWalkCount ์ด๊ธฐํ๋ ์๋์ด์๋ค