انسخ ما يلي
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class SoundAndMessag22eOddsnIntessraction1 : MonoBehaviour
{
public float interactionDistance = 1.0f;
public AudioClip soundToPlay;
public string interactionMessage = "Press E to Play";
private AudioSource audioSource;
private bool canInteract = false;
private Transform player;
private void Start()
{
audioSource = GetComponent<AudioSource>();
player = Camera.main.transform; // Assuming the player has a "Player" tag
}
private void Update()
{
if (player != null)
{
float distanceToPlayer = Vector3.Distance(player.position, transform.position);
// Check if the player is close enough to interact with the object
if (distanceToPlayer <= interactionDistance)
{
canInteract = true;
// Display the interaction message
if (canInteract && Input.GetKeyDown(KeyCode.E))
{
canInteract = false;
// Play the sound
audioSource.PlayOneShot(soundToPlay);
// Close the current scene and load the next scene
}
}
else
{
canInteract = false;
}
}
}
private void OnGUI()
{
if (canInteract)
{
GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2, 200, 40), interactionMessage);
}
}
}
ليست هناك تعليقات:
إرسال تعليق
عادة يتم الرد خلال ساعات