본문 바로가기
Unity/스크립트

케릭터이동 코드

by 세에레_freewing 2023. 3. 8.
반응형

Move.cs

 

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

public class Move : MonoBehaviour
{   
    public float movementSpeed = 3.0f;
    Vector2 movement = new Vector2();
    Rigidbody2D rigid;

    Animator animator;
        // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
        rigid = GetComponent<Rigidbody2D>();
       
    }
   
    // Update is called once per frame
    void Update()
    {
        UpdateState();
    }

    private void FixedUpdate() {
        MoveCharacter();    
    }

    private void MoveCharacter()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        movement.Normalize();

        rigid.velocity = movement * movementSpeed;

    }

    private void UpdateState()
    {
        if (Mathf.Approximately(movement.x, 0) && Mathf.Approximately(movement.y, 0))
        {
            animator.SetBool("Move", false);
        }
        else
        {
            animator.SetBool("Move", true);

        }
        animator.SetFloat("dir1", movement.x);
        animator.SetFloat("dir2", movement.y);
    }
}

 

반응형