DeveloperFeaturedGame DevelopmentUnity 3D

Unity 3D การทำระบบ กลางวัน กลางคืน โดยหมุน Directional Light

วิธีการง่ายๆ สำหรับการทำระบบ กลางวัน และกลางคืน (Day & Night) บน Unity 3D ด้วยภาษา C# ใช้หลักการหมุน หรือ Rotate ตัว Directional Light แบบง่ายครับ

Tutorial นี้ง่ายมากใช้ภาษา C# ครับ ส่วน Javascript ก็แปลงเอาง่ายๆ ล่ะกันนะ

ให้สร้างเกมแล้วออกแบบฉากตามสบายครับ

daynight-unity-2

กำหนด Directional Light ตั้งชื่อก็ได้ไม่ตั้งก็ได้ครับให้เป็นตามรูปแบบข้างล่างครับ ตามสะดวก

daynight-unity-3

ใช้ค่า Inspector เดียวกับผมก็ได้ครับ

daynight-unity-4

 

แสงปรกติก็จะเป็นตามนี้นะครับ

ให้ทำการเลือกที่ Directional Light ครับ สร้าง Scripts C# ลงไปว่า daynight.cs เขียน code ตามนี้

using UnityEngine;
using System.Collections;

public class GamePlay : MonoBehaviour {
	// Use this for initialization
	void Start () {
		Time.timeScale = 1;
	}
	// Update is called once per frame
	void Update () {
		transform.Rotate(Vector3.right * (Time.deltaTime*100));
	}
}

ในตัวอย่างนั้น ผมเร่งความเร็วในการหมุนแกน Directional Light หรือหมุนแกน x ของพระอาทิตย์ให้เร็ว โดยการ คูณ 100 เท่าเข้าไปครับ

transform.Rotate(Vector3.right * (Time.deltaTime*100));

หากลองทดสอบจะเห็นว่าแกน แสง จะหมุนรอบตัวเอง

daynight-unity-5

จบ ตัวอย่างนี้ ง่ายๆ แต่หลายคนคิดไม่ถึงว่าไหม? สำหรับสาย Javascript ก็

#pragma strict
function Start () {
	Time.timeScale = 1;
}
function Update () {
	transform.Rotate(Vector3.right * (Time.deltaTime*100));
}

ตามนั้นจ้า

Asst. Prof. Banyapon Poolsawas

อาจารย์ประจำสาขาวิชาการออกแบบเชิงโต้ตอบ และการพัฒนาเกม วิทยาลัยครีเอทีฟดีไซน์ & เอ็นเตอร์เทนเมนต์เทคโนโลยี มหาวิทยาลัยธุรกิจบัณฑิตย์ ผู้ก่อตั้ง บริษัท Daydev Co., Ltd, (เดย์เดฟ จำกัด)

Related Articles

Back to top button
Game & Mobile Development AR VR XR
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Adblock Detected

เราตรวจพบว่าคุณใช้ Adblock บนบราวเซอร์ของคุณ,กรุณาปิดระบบ Adblock ก่อนเข้าอ่าน Content ของเรานะครับ, ถือว่าช่วยเหลือกัน