์บ๋ฆญํฐ ์์ง์ด๊ธฐ
MovingObject.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingObject : MonoBehaviour
{
public float speed;// ์๋
private Vector3 vector; //x,y,z ์ถ
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// "Horizontal" : ์ฐ ๋ฐฉํฅํค : 1, ์ข ๋ฐฉํฅํค : -1 ๋ฆฌํด
// "Vertical" : ์ ๋ฐฉํฅํค : 1, ํ ๋ฐฉํฅํค : -1 ๋ฆฌํด
if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
{
vector.Set(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), transform.position.z); //z์ถ์ ์ ๋ฐ๋๋๊น ๊ทธ๋๋ก
// update ๋ง๋ค ํ์ฌ ์ค๋ธ์ ํธ์ ์์น์ ๋ํด์ค์ผ๋ก์จ ์ด๋
transform.position = transform.position + vector * speed;
}
}
}
์ ์คํฌ๋ฆฝํธ๋ฅผ ์บ๋ฆญํฐ ์ค๋ธ์ ํธ์ ๋ฃ์ด์ค๋๋ค.
- ์ธ์คํํฐ ์ฐฝ์์ speed ๋ฅผ ์ ํด์ค๋๋ค.
์ฌํํธ ๋๋ฅผ ์ ๋ฌ๋ฆฌ๊ธฐ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingObject : MonoBehaviour
{
public float speed;// ์ ํ ์๋
private Vector3 vector; //x,y,z ์ถ
public float runSpeed; // ์ ํ ๋ฌ๋ฆฌ๋ ์๋
private float applyRunSpeed; // ๋ฌ๋ฆด์ง ๋ง์ง์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๋ ์๋
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// "Horizontal" : ์ฐ ๋ฐฉํฅํค : 1, ์ข ๋ฐฉํฅํค : -1 ๋ฆฌํด
// "Vertical" : ์ ๋ฐฉํฅํค : 1, ํ ๋ฐฉํฅํค : -1 ๋ฆฌํด
if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
{
// ์ฌํํธ ๋๋ ์ ์
if (Input.GetKey(KeyCode.LeftShift))
{
applyRunSpeed = runSpeed;
}
else
{
applyRunSpeed = 0;
}
vector.Set(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), transform.position.z); //z์ถ์ ์ ๋ฐ๋๋๊น ๊ทธ๋๋ก
// update ๋ง๋ค ํ์ฌ ์ค๋ธ์ ํธ์ ์์น์ ๋ํด์ค์ผ๋ก์จ ์ด๋
transform.position = transform.position + vector * (speed + applyRunSpeed);
}
}
}