MIF_E31222344/Malukuu/Assets/Script/Bermain/drag_puzzle.cs

178 lines
3.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class drag_puzzle : MonoBehaviour
{
public GameObject detector;
public Vector3 pos_awal, scale_awal;
public bool on_pos = false, on_tempel = false;
// Tambahkan variabel untuk audio
public AudioClip audioBenar; // Audio saat benar
public AudioClip audioSalah; // Audio saat salah
private AudioSource audioSource;
// Start is called before the first frame update
void Start()
{
pos_awal = transform.position;
scale_awal = transform.localScale;
// Inisialisasi AudioSource
audioSource = GetComponent<AudioSource>();
if (audioSource == null)
{
audioSource = gameObject.AddComponent<AudioSource>();
}
}
void OnMouseDrag()
{
Vector3 pos_mouse = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z));
transform.position = new Vector3(pos_mouse.x, pos_mouse.y, -1f);
transform.localScale = new Vector2(55f, 55f);
}
void OnMouseUp()
{
if (on_pos)
{
// Jika object ditempatkan dengan benar
transform.position = detector.transform.position;
transform.localScale = new Vector2(55f, 55f);
on_tempel = true;
// Mainkan audio benar
if (audioBenar != null && audioSource != null)
{
audioSource.PlayOneShot(audioBenar);
}
}
else
{
// Jika object ditempatkan dengan salah
transform.position = pos_awal;
transform.localScale = scale_awal;
on_tempel = false;
// Mainkan audio salah
if (audioSalah != null && audioSource != null)
{
audioSource.PlayOneShot(audioSalah);
}
}
}
void OnTriggerStay2D(Collider2D objek)
{
if (objek.gameObject == detector)
{
on_pos = true;
}
}
void OnTriggerExit2D(Collider2D objek)
{
if (objek.gameObject == detector)
{
on_pos = false;
}
}
// Update is called once per frame
void Update()
{
}
}
// using System.Collections;
// using System.Collections.Generic;
// using UnityEngine;
// public class drag_puzzle : MonoBehaviour
// {
// public GameObject detector;
// public Vector3 pos_awal, scale_awal;
// public bool on_pos = false, on_tempel = false;
// // Start is called before the first frame update
// void Start()
// {
// pos_awal = transform.position;
// scale_awal = transform.localScale;
// }
// void OnMouseDrag()
// {
// Vector3 pos_mouse = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z));
// transform.position = new Vector3(pos_mouse.x, pos_mouse.y, -1f);
// transform.localScale = new Vector2 (55f, 55f);
// }
// void OnMouseUp()
// {
// if (on_pos)
// {
// transform.position = detector.transform.position;
// transform.localScale = new Vector2 (55f, 55f);
// on_tempel = true;
// }
// else
// {
// transform.position = pos_awal;
// transform.localScale = scale_awal;
// on_tempel = false;
// }
// }
// void OnTriggerStay2D(Collider2D objek)
// {
// if (objek.gameObject == detector)
// {
// on_pos = true;
// }
// }
// void OnTriggerExit2D(Collider2D objek)
// {
// if (objek.gameObject == detector)
// {
// on_pos = false;
// }
// }
// // Update is called once per frame
// void Update()
// {
// }
// }