Day 18 - pathway - Unity Essentials - Programming Essentials (More things to try)

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

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


Unity Essentials 의  네 번째 과정: Programming Essentials 에서

마지막에 나오는 More things to try 과정은 넘어가지 않고, 해본다..


Easy: Add a jump ability.

Medium: Make the door open when the player approaches. 

Expert: Use generative AI to add multi-camera toggling. 


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

    public float jumpForce = 5.0f;

    // 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()
    {
        if (Input.GetButtonDown("Jump")) {
            rb.AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
        }
    }

유니티에서 Jump button 에 해당되는 키는 Spacebar


자동 문열기를 위한 코드

Door GameObject 는 Animator component 를 가지고 있고, Door_Open 이라는 animation 이 지정되어 있다


public class DoorOpener : MonoBehaviour
{
   private Animator doorAnimator;

   void Start()
   {
       // Get the Animator component attached to the same GameObject as this script
       doorAnimator = GetComponent<Animator>();
   }

   private void OnTriggerEnter(Collider other)
   {
       // Check if the object entering the trigger is the player (or another specified object)
       if (other.CompareTag("Player")) // Make sure the player GameObject has the tag "Player"
       {
           if (doorAnimator != null)
           {
               // Trigger the Door_Open animation
               doorAnimator.SetTrigger("Door_Open");
           }
       }
   }
}


Door GameObject 가 가지고 있는

Animator component (충돌시 문 열리는 효과)

Box Collier component (충돌 처리, Is Trigger 체)





4. Expert: Use generative AI to simulate sunrise and sunset 에서는

Unity Muse, ChatGPT, or Claude 같은 AI large language models (LLMs) 을 이용하는 것을 소개


Unity Muse 는 Unity-specifi questions 를 위해서 trained and refined 된 것이라서 다른 일반적인 LLMs 보다 더 나은 결과를 줄 가능성이 높다

https://muse.unity.com/


ChatGTP 에서 아래 prompt 로 코드 생성하는 것이 나오는데, 영상에서 나오는 코드와는 조금 다르지만 결과는 비슷하게 나온다

“Create a script for Unity that I can add to my Directional Light that slowly rotates the light to simulate the day passing. The actual seconds for a day to pass should be a variable editable in the Inspector window.”

[ExecuteAlways] // Optional: allows it to run in edit mode too
public class DayNightCycle : MonoBehaviour
{
    [Tooltip("Duration of a full day in seconds.")]
    public float dayDurationInSeconds = 60f;

    private void Update()
    {
        if (dayDurationInSeconds <= 0f)
            return;

        // Degrees to rotate this frame
        float rotationSpeed = 360f / dayDurationInSeconds;
        float rotationThisFrame = rotationSpeed * Time.deltaTime;

        // Rotate around X-axis to simulate the sun's arc
        transform.Rotate(Vector3.right, rotationThisFrame, Space.Self);
    }
}


시험도 통과.. 역시 코딩은 할만함

이제 남은 것은 2D Essential 과 Publishing Essentials...

댓글

이 블로그의 인기 게시물

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

Unity Tip 2 : Visual Studio Code 로 변경

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