top of page

บันทึกการฝึกงานสหกิจศึกษา
รายละเอียดการทำงานของระบบหุ่นยนต์จำลอง
1. การใส่วิดีโอ
การใส่วิดีโอนั้นจะใช้วิธี MovieTexture Script ทำได้โดยการสร้าง 3D object ขึ้นมา -> import วิดีโอเข้ามาใน unity -> ลากวิดีโอไปใส่ใน 3D object -> add component -> audio source -> ลากชื่อ 3D object ในช่อง untitled ไปใส่ในช่อง audio source ของ video player
ตัวอย่างการใช้สคริป Ray Cast Check ในการตรวจจับวัตถุแล้วนำค่าการตรวจจับวัตถุมาเปิดวิดีโอ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayCastCheck : MonoBehaviour {
public RaycastHit hit;
public string Scara;
public string Articulate;
public string Cartesian;
public string Cylinti;
public float Reach = 2.0f;
public bool RayHit = false;
public GameObject Video;
public GameObject Room1;
public GameObject Room2;
public GameObject Room3;
public GameObject DoorL;
public GameObject DoorR;
//public Transform pp;
void Update()
{
var fwd = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, fwd * Reach, Color.red);
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Reach)) {
if (hit.transform.gameObject.name == Scara)
{
RayHit = true;
if (RayHit == true) {
Video.SetActive(true);
Room1.SetActive(false);
Room2.SetActive(false);
Room3.SetActive(false);
DoorR.SetActive(false);
DoorL.SetActive(false);
}
}
if (hit.transform.gameObject.name == Articulate)
{
RayHit = true;
if (RayHit == true)
{
Video.SetActive(false);
Room1.SetActive(true);
Room2.SetActive(false);
Room3.SetActive(false);
DoorR.SetActive(false);
DoorL.SetActive(false);
}
}
if (hit.transform.gameObject.name == Cartesian)
{
RayHit = true;
if (RayHit == true)
{
Video.SetActive(false);
Room1.SetActive(false);
Room2.SetActive(true);
Room3.SetActive(false);
DoorR.SetActive(false);
DoorL.SetActive(false);
}
}
if (hit.transform.gameObject.name == Cylinti)
{
RayHit = true;
if (RayHit == true)
{
Video.SetActive(false);
Room1.SetActive(false);
Room2.SetActive(false);
Room3.SetActive(true);
DoorR.SetActive(false);
DoorL.SetActive(false);
}
}
}
else
{
RayHit = false;
}
}
}
2. การใส่ควบคุมการทำงานของหุ่นเดินตาม
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class NPC : MonoBehaviour {
public GameObject ThePlayer;
public float TargetDistance;
public float AllowedDistance = 5f;
public GameObject TheNPC;
public float FollowSpeed;
public RaycastHit Shot;
public AudioClip voice;
public AudioSource voices ;
Rigidbody RIG;
// Use this for initialization
void Start () {
voices.clip = voice;
voices.Play();
RIG = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
CharacterController controller = GetComponent<CharacterController>();
Vector3 forward = transform.TransformDirection(Vector3.forward);
transform.LookAt (ThePlayer.transform);
var tmp = transform.rotation.eulerAngles;
tmp.x = 0;
tmp.z = 0;
transform.rotation = Quaternion.Euler(tmp);
var amp = ThePlayer.transform.position - TheNPC.transform.position;
TargetDistance = amp.magnitude;
if (TargetDistance >= AllowedDistance)
RIG.isKinematic = false;
FollowSpeed = 1f;
controller.SimpleMove(forward * FollowSpeed);
}
else
{
RIG.isKinematic = true;
FollowSpeed = 0f;
}
}
}
3. เปลี่ยนฉาก
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ChangScene : MonoBehaviour {
public int Scene;
public bool Load = false;
void Update()
{
if (Load == true)
{
SceneManager.LoadScene(Scene);
}
else { }
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Button L") {
Load = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Button L")
Load = false;
}
}
4. Score ของ mini game
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScroeBord : MonoBehaviour {
public Text text;
public static int coinAmount;
// Use this for initialization
void Start()
{
text = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
text.text = coinAmount.ToString();
}
}
bottom of page