본문 바로가기

Unity/Unity Learn

Junior Programmer > Unit 2 - Basic Gameplay

https://learn.unity.com/project/unit-2-basic-gameplay?language=en&pathwayId=5f7e17e1edbc2a5ec21a20af&missionId=5f71fe63edbc2a00200e9de0

 

Unit 2 - Basic Gameplay - Unity Learn

In this Unit, you will program a top-down game with the objective of throwing food to hungry animals - who are stampeding towards you - before they can run past you. In order to do this, you will become much more familiar with some of the most important pr

learn.unity.com

 

 

Make the projectile into a prefab

피자조각을 재사용하기 위해서 Prefab을 만들고 반복해서 사용할 수 있게 한다.

 

Create a new “Prefabs” folder, drag your food into it, and choose Original Prefab.

 






Launch projectile on spacebar press

Instantiate 메서드를 사용하여 플레이어 위치에 발사체를 생성한다.

Instantiate method

public static T Instantiate<T>(T original, Vector3 position, Quaternion rotation) where T : Object;

//Summary:
//    Clones the object original and returns the clone.

//Parameters:
//  original:
//    An existing object that you want to make a copy of.
//  position:
//    Position for the new object.
//  rotation:
//    Orientation of the new object.

 

if(Input.GetKeyDown(KeyCode.Space))
{
    //Launch a projectile from the player
    Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);
}

 





Destroy projectiles offscreen

피자 Prefab에 Add Component로 DestroyOutOfBounds 인스턴스를 추가하면 첫번째 투사체가 파괴되는 것을 확인 할 수 있다.

하지만 두 번째부터는 삭제되지 않는다.

 

In the Inspector Overrides drop-down, click Apply all to apply it to prefab

 

 

이렇게 Overrides로 발사되는 투사체에 모두 적용해 줘야 한다.

 

 



Change the perspective of the camera

2D로 내려다보는 효과를 주기위해 Orthographic으로 카메라 perspective 변경하기

 

 

Select the camera and change the Projection from “Perspective” to “Orthographic”





 

Spawn the animals at timed intervals

InvokeRepeating 메서드로 반복적으로 인스턴스를 Instantiate한다.

InvokeRepeating method

public void InvokeRepeating(string methodName, float time, float repeatRate);

//Summary:
//    Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds.

 

void Start()
{
    InvokeRepeating("SpawnRandomAnimal", startDelay, spawnInterval);
}

 





 

Destroy objects on collision

  • prefab의 Component에 Box Collider를 추가한다.
  • Is Trigger 체크박스에 체크를 한다.
  • OnTriggerEnter 메서드를 코드에서 override하여 Collider가 충돌했을 때 Destroy되는 로직을 추가한다. 
private void OnTriggerEnter(Collider other)
{
    Destroy(gameObject);
    Destroy(other.gameObject);
}

 

 

 

 

 

 

 

 

 

 

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

 

GitHub - Myoungmin/UnityLearn-Create_with_Code-Prototype_2: Unit 2 - Basic Gameplay

Unit 2 - Basic Gameplay. Contribute to Myoungmin/UnityLearn-Create_with_Code-Prototype_2 development by creating an account on GitHub.

github.com