[๐ฃ์ง๋ฌธ]
8๋ฐฉํฅ ์ด๋ ์บ๋ฆญํฐ ๊ด์ฑ ์ง๋ฌธ
๋ํธ์ด๋ณด(pon239)
2022-08-25 22:00
์ถ์ฒ 0

์ง๊ธ ์์ง์ ์์ฒด๋ ์์ฒญ ๋ง์กฑ์ค๋ฌ์ด๋ฐ, ํค๋ณด๋๋ฅผ ๋ผ๋ฉด ๋ฐ๋ก ๋ฉ์ถฐ๋ฒ๋ฆฌ๋๊ฒ ์ข ๊ทธ๋ผ.
๋ฉ์ถ๋ ํฌ๊ฒ๋ ์๋๋๋ผ๋ ์ฝ๊ฐ๋ง ๋ฐ๋ฆฌ๋ฉด์ ๋ฉ์ถ๋ฉด ์ฐธ ์ด์ ๊ฑฐ๊ฐ๋ค๋ ์๊ฐ์ด ๋ค์ด์ ์์ฌ์.
์์ง์ ์ ์ฒด์ ๊ด์ฑ์ ๋ฃ๊ธฐ ๋ณด๋ค๋ ๋ฉ์ถ ๋์๋ง ๋ฐ๋ฆฌ๊ฒ ๋ง๋ค๊ณ ์ถ์
์ฐพ์๋ณด๋๊น AddForce๋ผ๋ ๊ฑธ ์ด์ฉํด์ผ ํ๋ค๋๋ฐ, ์ด๊ฑธ ์ด์ฉํด์ 8๋ฐฉํฅ์ผ๋ก ์์ง์ด๊ฒ ํ ์ ์์?
ํ์ฌ ์ฝ๋๋ ์๋์ ์ฒจ๋ถํจ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SCR_Submarine_Movement : MonoBehaviour
{
public int speed;
private Rigidbody2D rigidBody;
private Animator animator;
private Vector2 vector;
// Start is called before the first frame update
void Start()
{
rigidBody = GetComponent
<Rigidbody2D
>();
animator = GetComponentInChildren
<Animator
>();
}
// Update is called once per frame
void Update()
{
vector.x = Input.GetAxisRaw("Horizontal");
vector.y = Input.GetAxisRaw("Vertical");
rigidBody.velocity = vector.normalized * speed;
if (vector.y
< 0)
{
animator.SetInteger("VerticalDirection", -1);
}
else if (vector.y
> 0)
{
animator.SetInteger("VerticalDirection", 1);
}
else if (vector.y == 0)
{
animator.SetInteger("VerticalDirection", 0);
}
}
}
vector์ magnitude๊ฐ 0์ด ์๋๋(์กฐ์์ค ์ผ๋)๋ง velocity๋ฅผ ๊ฑด๋ค๋ฉด ์์๋๋ ๋ง์ง๋ง ์ ๋ ฅ ์๋๋ก ์์ง์ด๊ฒ ๋ง๋ค์ ์์, ๊ฑฐ๊ธฐ์ vector์ magnitude๊ฐ 0์ด๋ฉด ๊ฐ์ํ๋ ์ฝ๋ ๋ฃ์ผ๋ฉด ๋์ง ์์๊น
์ ๋ํฐ 1์ผ์ฐจ๋ผ ์ ๋ชจ๋ฅด๋๋ฐ ์์ธํ ์ค๋ช ๊ฐ๋ฅ?
์ด์ค์ ์ดํด ์๋๋ ๋ถ๋ถ์ ๋งํด์ฃผ๋ฉด ๊ทธ๊ฑธ ์ค๋ช ํด์ค๊ป
์ ์ด์ ์๊ฑฐ๊ฐ์ ใณ
๊ณ ๋์์ง์ด์๋ค๋ฉด....
์ด๋ฐ๊ฑด ๋ฐฉ๋ฒ์ด ๋ง์ง ๋ฌด์ง์ฑ Vector3.Lerp์ ์ฉ, addForceํ๊ณ ์ ํญ ๋ง์ด๋ฃ๊ธฐ, ๊ทธ๋ฅ ์ฝ๋๋ก ์ ๋ถ ์ ์ดํ๊ธฐ ๋ฑ๋ฑ
ใณใณ