rigidbody가 없을 때 이동: Translate
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);
}
}
'유니티 기초' 카테고리의 다른 글
unity - 3d 기본 예제-밤송이 (0) | 2023.08.04 |
---|---|
Hero Jump 예제 (0) | 2023.08.03 |
애니메이션 전환 연습 (0) | 2023.08.03 |
Unity-Physics,Rigidbody, 스프라이트 애니메이션, ClimbCloud 예제 (0) | 2023.08.02 |
Unity-Prefab, 예제-랜덤한 위치에 화살생성, hp게이지 만들기 (0) | 2023.08.02 |