Unity 3D

สร้าง Cinematic บน Unity ฉาก Slow Motion Cut Scene

ตัวอย่างการสร้าง Cinematic ใน Unity 3D กับการทำ Slow Motion การเคลื่อนไหวใน Cut scene ของเกมที่เราสร้างขึ้น อาจจะมี Code เล็กน้อยแต่ไม่ยากเท่าไรนัก

เริ่มต้นให้ทำการ New Project ขึ้นมาเป็น 3D ในตัวอย่างใช้ Assets ของ Mixamo และ Asset Store ส่วนของ Hong Kong Builder Set เอามาจัดฉากเล็กน้อย

ทำการวาง Assets จัดฉากให้เรียบร้อย ตั้งค่า Animator ตัวละครให้เดินเข้าหากล้องพร้อมแสดง Animation เดินเท่ๆ เพิ่ม New Script เข้ามาคือ Walking.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Walking : MonoBehaviour {
	public float objectSpeed = 0.05f;
	// Use this for initialization
	void Start () {

	}

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

สร้าง Animator ใหม่ขึ้นมา

หลังจากนั้นหา Effect ระเบิดเช่น Simple Particle Pack หรือ อื่นๆ มาทำการเพิ่ม Component ส่วนของ Effects -> Legacy -> Ellipsoid Particle Emitter ใส่ลง Prefabs ให้เรียบร้อย

ตั้งค่า Ellipsoid Particle Emitter เสร็จแล้วให้ทำการเพิ่ม Script สำหรับสร้าง Slow Motion ใหม่ดังนี้:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SlowTime : MonoBehaviour {
	public bool isSlow = false;
	// Use this for initialization
	void Start () {
		Invoke ("Slow Motion",4f);
	}
	
	// Update is called once per frame
	void Update () {
		isSlow = true;
		Time.timeScale = 0.1f;
	}
}

เมื่อมีการระเบิด Time.timescale ของเกมจาก 1F จะถูกลดลงเป็น 0.1f ซึ่งเวลาของเกมจะเดินช้าลง ในขั้นตอนนี้เราตั้งค่า Bool ขึ้นมาตรวจสอบ true หรือ false ในตัวแปร isSlow นั่นหมายความว่าถ้าเกิดการระเบิดขึ้น Bool isSlow ตัวนี้จะมีค่าเป็น True ทันที เพื่อให้การทำงานของ Slow Motion เกิดขึ้นแล้วตัวละครต้องลด Speed การเคลื่อนไหวลง ดังนั้นต้องแก้ไขไฟล์ Walinkg.cs ดังนี้

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Walking : MonoBehaviour {
	public float objectSpeed = 0.05f;
	public SlowTime slowTime;
	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void Update () {
		transform.Translate (0, 0, objectSpeed);
		if (slowTime.isSlow) {
			objectSpeed = 0.01f;
		}
	}
}

ดึง Prefab ระเบิดที่เป็น Effects ที่มี code SlowTime.cs อยู่ไปวางใน Player ใน Script Walking.cs ให้เรียบร้อย ต่อมาเราจะทำการสั่งให้รถที่อยู่ในฉากระเบิดคลิกที่รถ

สร้าง Script ต่อไปนี้ Carbomb.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CarBomb : MonoBehaviour {
	public ParticleEmitter AttackEffect;
	public ParticleEmitter BigBomb;
	// Use this for initialization
	void Start () {
		Invoke ("Explode", 4f);
	}
	
	// Update is called once per frame
	void Update () {
		StartCoroutine (WaitExplode());

	}

	IEnumerator WaitExplode(){
		yield return new WaitForSeconds (2);
		Instantiate(AttackEffect, transform.position, transform.rotation);
		Instantiate(BigBomb, transform.position, transform.rotation);
		Destroy (this.gameObject);
	}
}

จับ Prefab ของระเบิดเข้าไปใส่ใน AttackEffect และ BigBomb ให้เรียบร้อยแล้วทำการทดสอบ

มาดูผลลัพธ์กันครับ

เสร็จล่ะการทำ Cinematic สำหรับ Cut Scene เท่ๆ ของเกมเรา

Asst. Prof. Banyapon Poolsawas

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

Related Articles

Back to top button

Adblock Detected

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