본문 바로가기
유니티강좌

왼쪽 Shift키 누르면 달리기 구현. 걷기 & 달리기. How to Character running when hold down Left Shift | 유니티 게임 개발 튜토리얼(Unity & C# Script)

by Ncube 2021. 1. 23.

 

 

캐릭터를 일반적인 속도로 상하좌우(WSAD)키로 이동시켜보고, 이동하면서 왼쪽 쉬프트키를 동시에 누렀을 때 지정한 속도로 달리기하는 캐릭터를 구현해봅니다. Unity & C# Script

 

 

[WalkAndRun.cs]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WalkAndRun : MonoBehaviour
{
    float speed, moveX, moveY;
    [SerializeField] float normalSpeed, runSpeed;

    void Update()
    {
        Movement();
    }

    void Movement()
    {
        if (Input.GetKey(KeyCode.LeftShift))
            speed = runSpeed;
        else
            speed = normalSpeed;

        moveX = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
        moveY = Input.GetAxis("Vertical") * Time.deltaTime * speed;
        transform.Translate(moveX, moveY, 0);
    }
}

 

 

댓글