아카이브

Hero Shooter 문열기, 애니메이션 적용 본문

유니티 심화

Hero Shooter 문열기, 애니메이션 적용

timbercat 2023. 8. 24. 18:24

1.포탈 위로 올라갔을때 문 열리게 하기

2. 이동 애니메이션 구현하기

 

Tutorial Main

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

public class TutorialMain : MonoBehaviour
{
    [SerializeField]
    private VariableJoystick joystick;

    [SerializeField]
    private Player player;

    [SerializeField]
    private Portal portal;

    [SerializeField]
    private Transform rightDoor;

    [SerializeField]
    private Transform leftDoor;

    private void Start()
    {
        this.portal.onReachPortal = () => {
            Debug.Log("You've reached the portal");
            //문열기
            Debug.Log("Move to the door");
            this.rightDoor.localRotation = Quaternion.Euler(0, -90, 0);
            this.leftDoor.localRotation = Quaternion.Euler(0, 90, 0);
        };

        //오른쪽 문 회전값 나오게하기
        //Debug.LogFormat("RightDoor World Rotation : {0}",  this.rightDoor.rotation.eulerAngles);
        //Debug.LogFormat("RightDoor Local Rotation : {0}", this.rightDoor.localRotation.eulerAngles);
        //this.rightDoor.localRotation = Quaternion.Euler(0, -90, 0);
    }
    void Update()
    {
        var h = this.joystick.Horizontal;
        var v = this.joystick.Vertical;
        var dir = new Vector3(h, 0, v);
        if (dir != Vector3.zero)
            this.player.Move(dir);
        else
            this.player.Idle();
    }
}

Player

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

public class Player : MonoBehaviour
{
    [SerializeField]
    private float moveSpeed = 2f;

    private Rigidbody rBody;

    public System.Action onReachPortal;

    private Animator anim;

    private void Start()
    {
        this.rBody = this.GetComponent<Rigidbody>();
        this.anim = GetComponent<Animator>();
    }

    public void Move(Vector3 dir)
    {
        this.anim.SetInteger("State", 1);
        this.rBody.isKinematic = false;
        var angle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg;
        var q = Quaternion.AngleAxis(angle, Vector3.up);
        this.transform.rotation = q;
        this.transform.Translate(Vector3.forward * this.moveSpeed * Time.deltaTime);
    }

    public void Idle()
    {
        this.anim.SetInteger("State", 0);
    }
}

Portal

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

public class Portal : MonoBehaviour
{
    public System.Action onReachPortal;
    private SphereCollider col;
    // Start is called before the first frame update
    void Start()
    {
        this.col = this.GetComponent<SphereCollider>();
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            this.col.enabled = false;
            this.onReachPortal();
        }
    }

'유니티 심화' 카테고리의 다른 글

[SaveAndLoad] 데이터 연동  (0) 2023.09.01
HeroShooter 적이 플레이어 추적  (0) 2023.08.29
Hero Shooter 조이스틱 이동, 포탈생성  (0) 2023.08.23
8/20 주말과제 궁수 이동, 공격  (0) 2023.08.21
총알발사  (0) 2023.08.18