โโโโ1. Collider์ ๋ฆฌ์ง๋๋ฐ๋๋ฅผ ์ถ๊ฐํ์ฌ ์ํ๋ ์ค์ ๊ฐ์ ๋ฃ๋๋ค.
1. ๋ง๋ค ๊ฒ์์ ์ค๋ ฅ์ด ์์ฉํ์ง ์์ผ๋ฏ๋ก RigidBody์์ ์ค๋ ฅ์ 0์ผ๋ก ์ค์
2. ์ ๋นํ ์ฌ์ด์ฆ๋ก ์ฝ๋ผ์ด๋ ํฌ๊ธฐ ์กฐ์
โโโโ2. ์ ๋ ฅ์ ๋ฐ๋ฅธ ์ด๋ ์คํฌ๋ฆฝํธ
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] private float speed;
private Vector2 inputVec;
private Rigidbody2D rigid;
private Collider2D col;
private void Awake()
{
rigid = GetComponent<Rigidbody2D>();
col = GetComponent<Collider2D>();
}
private void Update()
{
inputVec.x = Input.GetAxisRaw("Horizontal");
inputVec.y = Input.GetAxisRaw("Vertical");
}
private void FixedUpdate()
{
Vector2 nextVec = inputVec * speed * Time.fixedDeltaTime;
rigid.MovePosition(rigid.position + nextVec);
}
}
โโโโ3. ์ด๋ ๋ฐฉํฅ์ ๋ฐ๋ผ ์คํ๋ผ์ดํธ ๋ฐ์
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] private float speed;
private Vector2 inputVec;
private Rigidbody2D rigid;
private Collider2D col;
private SpriteRenderer spriter;
private void Awake()
{
rigid = GetComponent<Rigidbody2D>();
col = GetComponent<Collider2D>();
spriter = GetComponent<SpriteRenderer>();
}
private void Update()
{
inputVec.x = Input.GetAxisRaw("Horizontal");
inputVec.y = Input.GetAxisRaw("Vertical");
}
private void FixedUpdate()
{
Vector2 nextVec = inputVec * speed * Time.fixedDeltaTime;
rigid.MovePosition(rigid.position + nextVec);
}
private void LateUpdate()
{
if (inputVec.x != 0)
{
spriter.flipX = inputVec.x > 0;
}
}
}
โโโโ4. ์ ๋๋ฉ์ด์ ์ถ๊ฐ
์ฌ์ ์์
์ ์๋ ์ฌ์ง๊ณผ ๊ฐ์ด ์กฐ์ ํ๋ค.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] private float speed;
private Vector2 inputVec;
private Rigidbody2D rigid;
private Collider2D col;
private SpriteRenderer spriter;
private Animator anim;
private void Awake()
{
rigid = GetComponent<Rigidbody2D>();
col = GetComponent<Collider2D>();
spriter = GetComponent<SpriteRenderer>();
anim = GetComponent<Animator>();
}
private void Update()
{
inputVec.x = Input.GetAxisRaw("Horizontal");
inputVec.y = Input.GetAxisRaw("Vertical");
}
private void FixedUpdate()
{
Vector2 nextVec = inputVec * speed * Time.fixedDeltaTime;
rigid.MovePosition(rigid.position + nextVec);
}
private void LateUpdate()
{
// ๋จ์ Vector์ ํฌ๊ธฐ๋ก ์ ๋๋ฉ์ด์
์ ์ ์ฉํ๊ธฐ ์ํด magnitude๋ฅผ ์ฌ์ฉ
anim.SetFloat("Speed", inputVec.magnitude);
if (inputVec.x != 0)
{
spriter.flipX = inputVec.x > 0;
}
}
}
๊ณจ๋๋ฉํ ์ ํ๋ธ - ์ ๋ํฐ ๋ฑ์๋ผ์ดํฌ