Hi, I have a script like below, which is repating it self. But in PhonePickUp2 Methode Raycast can’t find or hit a phone target anymore. To this step script working fine. I mark this line with colour red. Thx for any advise or hints.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PhoneInteraction : MonoBehaviour
{
RaycastHit hit;
[SerializeField] float Distance = 4.0f;
[SerializeField] GameObject InteractionMessage;
private float RayDistance;
private bool CanSeeInteraction = false;
private bool phonePickedUp = false; // New flag to track phone pickup
private bool phonePickedUp2 = true;
public AudioSource phoneRinging;
public AudioSource phoneVoice1;
//public AudioSource phoneVoice2;
public AudioSource hangUp;
public AudioSource hangDown;
public AudioSource hangSound;
public GameObject phoneHead1;
public GameObject phoneHead2;
//public GameObject laptopPulpit;
// Start is called before the first frame update
void Start()
{
InteractionMessage.gameObject.SetActive(false);
RayDistance = Distance;
phoneHead1.SetActive(false);
phoneHead2.SetActive(true);
phoneRinging.Play();
}
// Update is called once per frame
void Update()
{
StartCoroutine(PhoneRinging());
//PhonePickUp();
}
IEnumerator PhoneRinging()
{
yield return new WaitForSeconds(5f);
PhonePickUp();
}
public void PhonePickUp()
{
if (!phonePickedUp && Physics.Raycast(transform.position, transform.forward, out hit, RayDistance))
{
if (hit.transform.CompareTag(“phone”))
{
CanSeeInteraction = true;
if (Input.GetKeyDown(KeyCode.E))
{
Debug.LogWarning(“Phone picked up”);
StartCoroutine(DisablePhoneHeadAfterAudioAndRestart());
//StartCoroutine(DisablePhoneHeadAfterAudio());
//phoneVoice1.Play();
// Set the flag to true when the phone is picked up
phonePickedUp = true;
CanSeeInteraction = false;
}
}
else
{
CanSeeInteraction = false;
}
}
if (CanSeeInteraction == true && !phonePickedUp)
{
InteractionMessage.gameObject.SetActive(true);
RayDistance = 1000f;
}
else
{
InteractionMessage.gameObject.SetActive(false);
RayDistance = Distance;
}
}
private IEnumerator DisablePhoneHeadAfterAudioAndRestart()
{
phoneHead2.SetActive(false);
phoneRinging.Stop();
hangUp.Play();
yield return new WaitForSeconds(1f);
phoneHead1.SetActive(true);
yield return new WaitForSeconds(1f);
phoneVoice1.Play();
yield return new WaitForSeconds(7f);
while (phoneVoice1.isPlaying)
{
yield return null;
}
yield return new WaitForSeconds(0.5f);
hangSound.Play();
yield return new WaitForSeconds(3f);
hangDown.Play();
phoneHead1.SetActive(false);
phoneHead2.SetActive(true);
yield return new WaitForSeconds(3f);
phoneRinging.Play();
PhonePickUp2();
}
public void PhonePickUp2()
{
phonePickedUp2 = false;
if (phonePickedUp2 == false)
{
Debug.LogWarning(“phonePickedUp2 set to false”);
}
else
{
Debug.LogWarning(“phonePickedUp2 set to true”);
}
Debug.LogWarning(“Starting PhonePickUp2 Methode”);
if (!phonePickedUp2 && Physics.Raycast(transform.position, transform.forward, out hit, RayDistance))
{
if (hit.transform.CompareTag(“phone”))
{
Debug.LogWarning(“Hitting Phone”);
CanSeeInteraction = true;
if (Input.GetKeyDown(KeyCode.E))
{
Debug.LogWarning(“Phone picked up”);
phoneRinging.Stop();
StartCoroutine(DisablePhoneHeadAfterAudioAndRestart2());
//StartCoroutine(DisablePhoneHeadAfterAudio());
//phoneVoice1.Play();
phonePickedUp2 = true; // Set the flag to true when the phone is picked up
CanSeeInteraction = false;
}
}
else
{
CanSeeInteraction = false;
}
}
if (CanSeeInteraction == true && !phonePickedUp2)
{
InteractionMessage.gameObject.SetActive(true);
RayDistance = 1000f;
}
else
{
InteractionMessage.gameObject.SetActive(false);
RayDistance = Distance;
}
}
private IEnumerator DisablePhoneHeadAfterAudioAndRestart2()
{
yield return new WaitForSeconds(3f);
phoneVoice1.Play();
}
}