K-digital traning/유니티 기초

캐릭터 이동(달리기) 애니메이션

내꺼블로그 2023. 8. 3. 14:37

플레이어의 기본 모션+달리는 모션을 연습해보았다,,,

 

1.AddForce로 구현

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

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

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        Debug.LogFormat("=>{0}", h);

        if (h != 0)
        {
            this.transform.localScale = new Vector3(h, 1, 1);
        }

        this.anim.SetInteger("Direction", (int)h);


        //AddForce 활용한 방법
        if (h != 0)
        {
            var force = new Vector3(h, 0, 0) * moveForce;
            Debug.LogFormat("force: {0}", force);   //0벡터로 민다 
            this.rBody2D.AddForce(force);
        }
        else
            this.rBody2D.velocity = Vector2.zero;
    }
}

 

 

*Idle -> Run

Direction이 0이 아닐 때(방향을 가지고 있을 때) Run Animation으로 transition

 

 

*Run -> Idle

Direction이 0일 때(방향X 정자세) Idle Animation으로 transition

 

 

 

 

 

2.RigidBody2D의 MovePosition 사용

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

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

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        Debug.LogFormat("=>{0}", h);

        if (h != 0)
        {
            this.transform.localScale = new Vector3(h, 1, 1);
        }

        //this.anim.SetInteger("Direction", (int)h);


        //Rigidbody MovePosition 활용한 방법
        Vector2 dir = new Vector2(h, 0);
        Vector2 movement = dir * moveSpeed * Time.deltaTime;
        this.rBody2D.MovePosition(this.rBody2D.position + movement);
        this.anim.SetInteger("Direction", (int)h);
    }
}

 

 

 

 

 

 

 

 

 

3.velocity 조정

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

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

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        Debug.LogFormat("=>{0}", h);

        if (h != 0)
        {
            this.transform.localScale = new Vector3(h, 1, 1);
        }


        float velocityX = moveSpeed * h;
        this.rBody2D.velocity = new Vector2(velocityX, 0);
        this.anim.SetFloat("Speed", Mathf.Abs(this.rBody2D.velocity.x));
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

결과

'K-digital traning > 유니티 기초' 카테고리의 다른 글

몬스터 처지 후 아이템 드롭 + 장착  (0) 2023.08.10
[주말과제] 이동 및 몬스터 공격  (0) 2023.08.07
애니메이션 전환 연습  (0) 2023.08.03
ClimbCloud  (0) 2023.08.02
[수업과제] 표창 던지기  (0) 2023.08.01