Unity_Animation : player #2

haechi·2021년 9월 13일
0

unity

목록 보기
39/39

210913
Unity_Animation_player #2


지난 애니메이션 -> 앉기까지 구현

앉기 모션이 있다면?
일어서기도 있어야하고 앉은 상태로 대기하는 모션도 있어야한다.


-PlayerController.cs

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

public class PlayerController : MonoBehaviour
{
    //private Movement2D movement2D;
    private Animator animator;  // Animator 타입의 변수
    private string stance = "stand";
    private void Awake()
    {
        //movement2D = GetComponent<Movement2D>();
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
       // float x = Input.GetAxisRaw("Horizontal");   // 좌 우 입력
       /* movement2D.Move(x); // 입력받은 float x로 이동방향 제어*/
        

        // 스페이스 키를 누르면 점프
        /*if (Input.GetKeyDown(KeyCode.Space))
        {
            movement2D.Jump();
        }
*/
        if (Input.GetKeyDown(KeyCode.C) && stance == "stand")   // C 입력 and 상태 stand => 앉기
        {
            animator.SetTrigger("sit");
            stance = "sit";
        }
        else if (Input.GetKeyDown(KeyCode.C) && stance=="sit") // C 입력 and 상태 sit => 일어서기
        {
            animator.SetTrigger("stand");
            stance = "stand";
        }
    }
}

Player_Idle상태에서 c를 입력하면 sit 상태가 되고 다시 입력시 일어서도록 하려고 했으나 각 if문 마다 상태에 따라서 했을 때 sit과 stand모두 적용이 되어서 if else if로 만들었고 SetTrigger를 통해서 모션 전환을 하도록 했다. 스탠스 변경이 되는 경우 stance변수 값에 각 값을 넣어주어 바로 일어서거나 하는 경우를 막았다.

일어서는 애니메이션의 경우 앉는 애니메이션의 반대로 진행하도록 넣어주었다.

idle상태 -> c입력 -> sit Trigger -> sit_idle -> c입력 -> stand Trigger -> player_idle

  • 걷기 추가
    우선 현재 플레이어가 왼쪽을 바라보고 있어서 left_walk를 추가했다. 방향전환같은 경우는 추후 작업할 예정이다.

걷기인 만큼 속도를 좀 더 낮게 조절했다.

-Movement2D.cs

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

public class Movement2D : MonoBehaviour
{
    [SerializeField]
    private float speed = 1.0f; // 속도   
    [SerializeField]
    private float jumpForce = 8.0f; // 점프 힘 (클수록 높이 점프한다)
    [SerializeField]
    private float gs = 1.5f;
    private Rigidbody2D rigid2D;

    [SerializeField]
    private LayerMask groundLayer;  // 바닥 체크를 위한 충돌 레이어
    private BoxCollider2D BoxCollider2D; // 오브젝트의 충돌 범위 컴포넌트
    private bool isGrounded;    // 바닥에 닿아있을 때 true이다
    private Vector3 footPosition;   // 발의 위치


    private void Awake()
    {
        rigid2D = GetComponent<Rigidbody2D>();  // Rigidbody2D의 컴포넌트를 가진다.
        BoxCollider2D = GetComponent<BoxCollider2D>();  // CapsuleCollider2D의 컴포넌트를 가진다.
    }
    private void FixedUpdate()
    {
        // 플레이어 오브젝트의 Collider2D min, center, max 위치 정보
        Bounds bounds = BoxCollider2D.bounds;
        // 플레이어 발 위치 설정
        footPosition = new Vector2(bounds.center.x, bounds.min.y);
        // 발 위치에 원 생성, 원이 바닥과 닿아있으면 true
        isGrounded = Physics2D.OverlapCircle(footPosition, 0.1f, groundLayer);


        rigid2D.gravityScale = gs;    // 점프 높이 조절
    }

    public void Move(float x)   // 좌우 이동
    {
        // x 축 이동은  x * speed, y 축은 기존의 속력 값
        rigid2D.velocity = new Vector2(x * speed, rigid2D.velocity.y);
    }
    public void Jump()  // 점프
    {
        if(isGrounded == true)
        {
            // fumpForce의 크기만큼 위 방향으로 속력 설정
            rigid2D.velocity = Vector2.up * jumpForce;
        }
    }
}

그리고 PlayerController.cs의 코드에서 A입력을 하면 좌로 이동하도록 한다. 키를 떼게 되면 정지하도록 했다.

모션이 끝까지 실행되지 않고 누른만큼만 움직이기 위해서 Has Exit Time의 값을 false로 설정했다.

걷기의 모션이 다시 처음 상태로 돌아오는 모션이라 끝맺음이 조금 아쉽지만 다음에 애니메이션을 만들 때 좀 더 보완하면 좋을 듯 하다.

profile
공부중인 것들 기록

0개의 댓글