본문 바로가기

카테고리 없음

유니티 플레이어 리기드바디 이동/ 유니티 캐릭터 이동 구현/ 유니티 보는 방향으로 회전 이동

728x90

케릭터가 방향을 입력받아 입력받은 방향으로 객체를 회전하여 이동하는 스크립트입니다

충돌 옵션을 위해 리기드로 구현하였습니다

 

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

public class Player : MonoBehaviour
{
    public float moveSpeed;
    private Rigidbody charRigidbody;

    void Start()
    {
        charRigidbody = GetComponent<Rigidbody>();
    }

    void Update()
    {
        float hAxis = Input.GetAxisRaw("Horizontal");
        float vAxis = Input.GetAxisRaw("Vertical");

        Vector3 inputDir = new Vector3(hAxis, 0, vAxis).normalized;

        charRigidbody.velocity = inputDir * moveSpeed;

        transform.LookAt(transform.position + inputDir);
    }
}

728x90