rigidbody가 없을 때 이동: Translate
2023. 8. 3.

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

public class HeroController : MonoBehaviour
{
    Animator anim;
  //  private Rigidbody2D rBody2D;
    public float moveForce = 1f;
    public float moveSpeed = 1f;
    // Start is called before the first frame update
    void Start()
    {
        this.anim = GetComponent<Animator>();
      //  this.rBody2D = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {

        float h = Input.GetAxisRaw("Horizontal");   //-1, 0, 1
        Debug.LogFormat("=> {0}", (int)h);
        //좌우 반전 , 왼쪽 h값이 -1 
        if (h != 0)
        {
            this.transform.localScale = new Vector3(h, 1, 1);
        }

        //리지드 바디가 없을 경우 이동: Translate
        Vector3 dir = new Vector3(h, 0, 0);
        float speed = 1f;
        this.transform.Translate(dir*speed*Time.deltaTime);

        //애니메이션 전환
        //멈춤: 기본동작- h값이 0, 즉 dir 값이 (0,0,0) 벡터

        //이동: 뛰는 동작 - h값이 0이아니다: -1,1

        //Animator의 매개변수에 값을 할당하면
        //Animator Controller의 transition의 condition에 의해 전환됨-> 컨디션 설정
        this.anim.SetInteger("h", (int)h);
    }

}
myoskin