본문 바로가기

Unity/Unity Learn

Junior Programmer > Unit 4 - Gameplay Mechanics

https://learn.unity.com/project/unit-4-gameplay-mechanics?uv=2020.3&pathwayId=5f7e17e1edbc2a5ec21a20af&missionId=5f7648a4edbc2a5578eb67df

 

Unit 4 - Gameplay Mechanics - Unity Learn

In this Unit, you will program an arcade-style Sumo battle with the objective of knocking increasingly difficult waves of enemies off of a floating island, using power ups to help defeat them. In creating this prototype, you will learn how to implement new

learn.unity.com

 

 

Create a focal point for the camera

Empty Object를 만들고 Main Camera를 Hierarchy상에서 이 모브젝트의 하위에 놓는다.

이 오브젝트 이름을 Focal Point로 이름을 설정한다.

 

 

Main Camera가 Focal Point의 child object가 되어 Focal Point의 rotation 값이 바뀌면 Main Camera의 rotation또한 변경된다.





Move in direction of focal point

Global과 Local 키

 

Local로 선택한 후 Main Camera의 시점을 변경하면 그 시점에 맞춰 좌표축이 변경된다.

객체마다 값을 변경하며 확인하기에 좋다.

 

Global로 선택하면 절대 좌표축으로 설정된다.

Scene과 주변 환경을 설정할 때 동일한 좌표를 확인하면서 설정할 때 좋다.



시점 방향으로 이동하도록 설정하기

initialize it in Start(): focalPoint = GameObject.Find("Focal Point");

 

void Start()
{
    playerRb = GetComponent<Rigidbody>();
    focalPoint = GameObject.Find("Focal Point");
}

 

In the AddForce call, Replace Vector3.forward with focalPoint.transform.forward

 

void Update()
{
    float forwardInput = Input.GetAxis("Vertical");
    playerRb.AddForce(focalPoint.transform.forward * speed * forwardInput);
}




 

Add an enemy and a physics material

공이 튕기는 효과를 주기 위해 physics material를 Sphere Collider에 적용한다.

 

Physics Material 생성하여 Bounciness와 Bounce Combine을 변경한다.

 



Sphere Collider의 Material에 적용

 

 

 

 

 

Create enemy script to follow player

Enemy가 Player 방향으로 계속 이동하려면 서로의 위치를 이용하여 Vector를 계산해야 한다.

Player 위치에서 Enemy의 위치를 빼주면 Enemy가 이동해야하는 Vector3 Coordinates를 구할 수 있다.

 

Player위치와 Enemy의 위치가 멀수록 더 강한 힘이 가해지는 것을 방지하기 위해 normalized를 이용하여 거리와 상관없이 동일한 힘이 가해지도록 설정해준다.

 

void Update()
{
    Vector3 lookDirection = (player.transform.position - transform.position).normalized;
    enemyRb.AddForce(lookDirection * speed);
}




Create Countdown Routine for powerup

한정된 시간동안만 효과를 주기위해 coroutine을 사용한다.

Update() 루프를 실행하는 스레드와 별도의 스레드를 돌면서 타이머 동작을 위해 아래와 같은 코드를 구성한다.

 

private void OnTriggerEnter(Collider other)
{
    if(other.CompareTag("Powerup"))
    {
        hasPowerup = true;
        Destroy(other.gameObject);
        StartCoroutine(PowerupCountdownRoutine());
    }
}

IEnumerator PowerupCountdownRoutine()
{
    yield return new WaitForSeconds(7);
    hasPowerup = false;
}

 

StartCoroutine 메서드에서 IEnumerator를 반환하는 메서드를 파라미터로 받는다.

yield return new WaitForSeconds(7);를 통해 해당 시간동안 타이머가 돌고

그 다음에 bool값을 변경하는 코드가 실행된다.

 

 

 

 

 

 

https://github.com/Myoungmin/UnityLearn-Create_with_Code-Prototype_4

 

GitHub - Myoungmin/UnityLearn-Create_with_Code-Prototype_4: Unit 4 - Gameplay Mechanics

Unit 4 - Gameplay Mechanics. Contribute to Myoungmin/UnityLearn-Create_with_Code-Prototype_4 development by creating an account on GitHub.

github.com