Day 14 ~ Day 17 - pathway - Unity Essentials - Programming Essentials

 Unity Essentials - Editor Essentials 읽어보면서 정리한 거

https://learn.unity.com/pathway/unity-essentials?version=6


Unity Essentials 의  네 번째 과정: Programming Essentials



4_LivingRoom_Programming_Scene 에서


❗처음에 만든 Script file 명을 나중에 바꾸지 마라. 나중에 문제 일으킬 수 있다.

이름을 바꾸고 싶으면, 삭제 후 새로 만들어라가 가이드


MainCamera 를 Player GameObject 의 child 로 넣고 아래와 같이 해두면

진행 방향 뒤에서 보는 식으로 되네.. 신기..




... position the camera to follow the player in a third-person view, which will be more immersive and intuitive.


❗연결된 스크립트의 Public 속성값을 Play 모드에서도 변경하는 경우에 바로 바로 적용이 되어서 적절한 값을 찾는데 도움이 되지만, Play 모드가 종료되면 값이 사라진다. 기억해 두어야 할 속성값이 많으면 Play 모드에서 Copy Component 해두고, 종료 후에 Paste Component 해두면 값이 그대로 적용 된다


PlayerController.cs

A Rigidbody provides a physics-based way to control the movement and position of a GameObject.

https://docs.unity3d.com/Manual/class-Rigidbody.html

using UnityEngine;

// Controls player movement and rotation.
public class PlayerController : MonoBehaviour
{
    public float speed = 5.0f; // Set player's movement speed.
    public float rotationSpeed = 120.0f; // Set player's rotation speed.

    private Rigidbody rb; // Reference to player's Rigidbody.

    // Start is called before the first frame update
    private void Start()
    {
        rb = GetComponent<Rigidbody>(); // Access player's Rigidbody.
    }

    // Update is called once per frame
    void Update()
    {
       
    }


    // Handle physics-based movement and rotation.
    private void FixedUpdate()
    {
        // Move player based on vertical input.
        float moveVertical = Input.GetAxis("Vertical");
        Vector3 movement = transform.forward * moveVertical * speed * Time.fixedDeltaTime;
        rb.MovePosition(rb.position + movement);

        // Rotate player based on horizontal input.
        float turn = Input.GetAxis("Horizontal") * rotationSpeed * Time.fixedDeltaTime;
        Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
        rb.MoveRotation(rb.rotation * turnRotation);
    }
}


4. Review the structure of the default script 에서 기본 코드 설명


GameObject 를 선택하고, 


Global 에서 Local 로 바꾸면

Transform 에서 Rotation 의 Y 값을 바꾸면 그에 대한 변화가 바로 Scene 에서 보임

Global 로 되어 있으면 바꿔도 Scene 에서 업데이트 안됨


✅ Shift 를 누른 상태에서 Y 값을 드래그 (drag) 하게 되면 값이 더 크게 변경


    // Update is called once per frame
    void Update()
    {
        transform.Rotate(0, 1, 0);
    }

=> Unity runs this function once per frame

     Games typically run at 30-60 frames per second

     Rotate the collectible object 1 degree around the Y-axis in each frame,


✅ Play mode 에서도 Scene 로 전환해서 동작하는 모습을 볼 수 있다


public member variable 을 통해서 inspector window 에서 값을 지정할 수 있다


2. Allow the player to go through the collectible 에서


Collider component 의 IsTrigger 속성


Collider 컴포넌트가 물리적인 충돌 대신에 trigger 처럼 행동하도록 설정하는 것

When you tell a collider component to act as a trigger instead of a physical collider, it will no longer have a sold barrier, but it can still detect entry by the player or other GameObjects.


3. Add the OnTriggerEnter function 에서


OnTriggerEnter is only called if at least one of the objects involved has a Rigidbody component attached to it.


5. Destroy the collectible 에서


    private void OnTriggerEnter(Collider other) {
       
        // Destroy the collectible
        Destroy(gameObject);
    }

여기서 GameObject 는 script 가 attach 된 그 GameObject 를 말한다..




6. Explorer particle effect options 에서


Visual Effects (VFX) - like puffs of smoke or mini explosions


유니티에서는 불, 연기, a burst of sparks 와 같은 복잡한 visual 효과를 시뮬레이션하기 위해서 particle systems 를 이용한다


Particles panel



7. Create and assign a new variable for the particle 에서


    public GameObject onCollectEffect;

Hold the reference to your particle effect prefab


스크립트에서 GameObject type 의 변수를 추가하고, prefab 에서 드래그 & 드랍으로 지정해준다



8. Instantiate the partcle 에서


    private void OnTriggerEnter(Collider other) {
       
        // Destroy the collectible
        Destroy(gameObject);

        // Instantiate the particle effect
        Instantiate(onCollectEffect, transform.position, transform.rotation);
    }

❗ Instantiating an object in Unity means creating a copy of it during runtime (while the application is running); your're making a new instance of that prefab in your scene.

한글로 쓰기 힘들어...😂

Instantiate 메소드는 아래 정보를 필요

- Which object to instantiate

- The position of the instantiated object

- The rotation of the instantiated object


collectible GameObject 의 transform (position, scale, rotation) 을 참조하면 된다....


10. Restrict collisions to the player only 에서


유니티에서는 tags 라고 불리는 피쳐(feature) 를 사용해서 플레이어 GameObject 를 구별할 수가 있다...


    private void OnTriggerEnter(Collider other) {
       
        if (other.CompareTag("Player")) {
            // Destroy the collectible
            Destroy(gameObject);

            // Instantiate the particle effect
            Instantiate(onCollectEffect, transform.position, transform.rotation);
        }
    }

When an object with a Rigidbody component collides with me, IF that object is the player, then destory myself and spawn a partcle effect

Player GameObject 는 Rigidbody component 를 가지고 있고, 나(Dirt) 와 충돌을 하는 경우에 tag 값을 보고 Player 인 경우에만 처리 코드가 동작하게 한다.....











댓글

이 블로그의 인기 게시물

나도코딩 - 유니티 무료 강의 (Crash Course) 따라하기

Unity Tip 2 : Visual Studio Code 로 변경

노마드 코더 (Nomad Coders) 강의 따라하기 - KIMCHI-Run 게임