UNITY_C#

[유데미x스나이퍼팩토리]4회차_0918_(Unity) 10주 완성 프로젝트 캠프_도트먹기게임,시점특징유니티

쫑나리 2023. 9. 18. 17:40
728x90
반응형
SMALL

충돌(collision)
트리거(trigger)
중력(rigidbody)
* if문 안에서는 float보다는 int형을 사용해주는 것이 좋음
* switch case문은 분기처리할 때 적극 활용하기
 
오늘 강의는 딜리셔스게임즈 이현진 대표님께서 강의해주셨습니다!

시점 정리
1인칭 시점_ fps게임,VR에 많이 사용되고 몰입도가 높고 실감나는 플레이가 장점
하지만 멀미가 난다거나 거리감에 좋지않다는 단점
3인칭 시점_ (탑뷰, 쿼터뷰, 숄더뷰or백뷰, 사이드뷰)
숄더뷰_ 1인칭 시점의 단점을 보완하여 좀 더 역동적인 액션을 즐길 수 있다.
- 거리감이 좋은 것은 탑뷰/사이드뷰
탑뷰_ 위에서 아래 전체를 내려다 보이는 뷰
사이드뷰_ 옆에서 보는 시점으로 철권과 스트리트파이트
쿼터뷰_ 거리감과 볼륨감을 동시에 챙길 수 있다. 하지만 왜곡이 심하다는 점
 
빈오브젝트 Player를 생성했고, 아래 유니티짱을 넣어주었고, 
바닥과 미로 오브젝트를 만들어주었다.
Player에 Add Component - Character Controller 

Player 스크립트 생성 후 Player에 넣어줌

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

public class Player : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float rotationSpeed = 360f;
    CharacterController charCtrl;
    // Start is called before the first frame update
    void Start()
    {
        charCtrl = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 dir = new Vector3(
            Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        if (dir.sqrMagnitude > 0.01f)
        {
            Vector3 forward = Vector3.Slerp(transform.forward, dir,
            rotationSpeed * Time.deltaTime / Vector3.Angle(transform.forward, dir));
            transform.LookAt(transform.position + forward);
        }
        charCtrl.Move(dir * moveSpeed * Time.deltaTime);
    }
}

 
Animations 폴더 생성 후 Player Animation 생성 

 

Wait와 Run에 모션을 줌

 

 
Dot를 만들고 Material도 주었음 + 프리팹화

    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("dot!");
    }
콘솔에서도 확인해본다. 도트에 닿으면 콘솔창에서 Dot가 잘 확인 됩니다.
도트를 잘 배치하고

    private void OnTriggerEnter(Collider other)
    {
        Destroy(other.gameObject);
    }
도트와 닿으면 사라질 수 있도록 Destroy해줍니다. 잘 사라지는 것을 확인하고,
남은 도트를 확인하기 위해 업데이트에 넣어줍니다.
if (GameObject.FindGameObjectsWithTag("Dot").Length < 1) SceneManager.LoadScene(SceneManager.GetActiveScene ().name);
 
적 생성하기

 
바닥과 미로를 navigation static 으로 설정해줌

그리고 Ai Navigation이 벽으로 인식할 수 있도록 벽과 바닥을 Bake 해줌
아까 생성한 Enemy 스크립트를 만들어줌

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

public class Enemy : MonoBehaviour
{
    public GameObject target;
    NavMeshAgent agent;

    // Start is called before the first frame update
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();

    }

    // Update is called once per frame
    void Update()
    {
        agent.destination = target.transform.position;

    }
}

타겟에 플레이어를 넣어줌

 
적 애니메이터 추가

플레이어와 같도록 스크립트 수정

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

public class Enemy : MonoBehaviour
{
    public GameObject target;
    NavMeshAgent agent;
    Animator anim;


    // Start is called before the first frame update
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        anim = GetComponentInChildren<Animator>();

    }

    // Update is called once per frame
    void Update()
    {
        agent.destination = target.transform.position;
        anim.SetFloat("Speed", agent.velocity.magnitude);

    }
}

트리거 추가 / Enemy 태그 추가

잘 따라댕깁니다.

 
Player.cs 스크립트에 재시작 부분을 수정해줍니다.

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

public class Player : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float rotationSpeed = 360f;

    CharacterController charCtrl;   //변수화 하여 사용
    Animator anim;

    // Start is called before the first frame update
    void Start()
    {
        charCtrl = GetComponent<CharacterController>();
        anim = GetComponentInChildren<Animator>();

    }

    // Update is called once per frame
    void Update()
    {
        Vector3 dir = new Vector3(
            Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        if (dir.sqrMagnitude > 0.01f)
        {
            Vector3 forward = Vector3.Slerp(transform.forward, dir,
            rotationSpeed * Time.deltaTime / Vector3.Angle(transform.forward, dir));
            transform.LookAt(transform.position + forward);
        }
        charCtrl.Move(dir * moveSpeed * Time.deltaTime);
        anim.SetFloat("Speed", charCtrl.velocity.magnitude);

        if (GameObject.FindGameObjectsWithTag("Dot").Length < 1)
            SceneManager.LoadScene(SceneManager.GetActiveScene
            ().name);

    }
    private void OnTriggerEnter(Collider other)
    {
        switch (other.tag)
        {
            case "Dot":
                Destroy(other.gameObject);
                break;
            case "Enemy":
                SceneManager.LoadScene(SceneManager.GetActiveScene().name);
                break;
        }
    }

}

 

728x90
반응형
LIST