TIL

[멋쟁이사자처럼 부트캠프 TIL 회고] 유니티 게임개발 3기 8일차 캐릭터 애니메이션, Light

HYttt 2024. 11. 28. 17:26

걷는 애니메이션 만들기

  • 캐릭터 다리의 Pivot을 재설정

  • 캐릭터의 다리 각도를 조절하여 포인트 설정

  • 이동 키를 누르고 있을 때 애니메이션이 재생되도록 Animator에서 isWalk 변수와 transition을 설정

  • NewState -> CharacterMove 트랜지션에 isWalk = true
  • CharacterMove -> NewState 트랜지션에 isWalk = false

  • 이동을 멈추면 즉시 애니메이션을 종료하기위해 두 트랜지션 모두 HasExitTime을 해제하고 Duratior값을 0으로 변경
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.TextCore.Text;
using UnityEngine.UIElements;

public class Movement : MonoBehaviour
{

    private float h = 0f;
    private float v = 0f;
    public float moveSpeed = 1f;
    public float jumpPower = 10f;
    public bool isGround = false;
    public Animator anim;	//애내메이터 접근 변수

    void Update()
    {
        Move();
        Jump();
    }

    private void Jump()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isGround == true)
        {
            GetComponent<Rigidbody>().AddForce(Vector3.up * jumpPower, ForceMode.Impulse);
        }
    }
    private void Move()
    {
        h = Input.GetAxis("Horizontal");  
        v = Input.GetAxis("Vertical");    
        Vector3 dir = new Vector3(h, 0, v).normalized;

        transform.position += dir * moveSpeed * Time.deltaTime;
        if (h == 0 && v == 0)	//키를 누르지 않을 때
        {
            anim.SetBool("isWalk", false);	//애니메이터의 isWalk변수를 false로
        }
        else					//키를 누르고 있을 때
        {
            anim.SetBool("isWalk", true);	//애니메이터의 isWalk변수를 true로
        }

        if (h != 0 || v != 0)
        {
            
            Vector3 targetPos = transform.position + dir; 
            transform.LookAt(targetPos);
        }
    }
}

Movement.cs

  • 애니메이션의 처음과 끝이 blend되어 반복될 때 딜레이가 있으므로 그래프의 기울기를 수정

수정 전
수정 후

라이트

  • Direction Light : 직선의 모든 범위

  • Point Light : 구 모양

  • Spot Light : 콘 모양

  • Area Light : 사각형/타원 모양, Static설정 필요

 

낮과 밤

  • DirectiionalLight에 적용
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LightRotation : MonoBehaviour
{
    public float rotSpeed = 10f;

    void Update()
    {
        transform.Rotate(Vector3.right * rotSpeed * Time.deltaTime);
    }
}

 

손전등 On / Off

https://assetstore.unity.com/packages/3d/props/electronics/flashlight-18972

 

Flashlight | 3D 전자제품 | Unity Asset Store

Elevate your workflow with the Flashlight asset from RRFreelance. Find this & other 전자제품 options on the Unity Asset Store.

assetstore.unity.com

  • Flashlight의 자식으로 SpotLight 오브젝트 생성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Flashlight : MonoBehaviour
{
    public GameObject spotLight;
    public bool isFlasLight = false;
    private void Start()
    {
        isFlasLight = false;
        spotLight.SetActive(isFlasLight);
    }
    private void Update()
    {
        //Light On/Off -> 오브젝트의 존재 On/Off
        if (Input.GetKeyDown(KeyCode.F))
        {
            isFlasLight = !isFlasLight;
            spotLight.SetActive(isFlasLight);
            //Debug.Log(isFlasLight + "딸깍!");
        }
    }
}
  • SpotLight 오브젝트에 스크립트 적용
  • F를 누르면 On / Off

 

Cookie 이미지

  • Cookie 이미지를 사용하면 이미지 모양의 빛을 만들 수 있다