Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Second hit transform - raycast never find a target.

Discussion in 'Scripting' started by wernetto, Aug 24, 2023.

  1. wernetto

    wernetto

    Joined:
    Jun 10, 2022
    Posts:
    3
    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();

    }
    }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Please don't post code as a wall of plain text. Please edit your existing post to use code-tags.

    To note, code like this isn't easy to debug in your head so other devs that don't have the project will unlikely be able to help. You have the project so we can only presume you've debugged it already so you should mention what the results were from that (I see debug output being done but no mention).

    If it's not detecting something then it'll be because something isn't there or you're not passing in to the query what you think you are so debug the arguments to it.

    Thanks.
     
    CodeSmile likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Also to note, you're starting a new coroutine every frame which waits for 5 seconds! You'll have thousands of these running.
     
  4. wernetto

    wernetto

    Joined:
    Jun 10, 2022
    Posts:
    3
    Hi, thx for help, because putting Coroutine in Update it was bad idea ;) I've just made one methode for PickUp interaction in Update right now. So entire logic of picking up phone and different conversation is in this one methode PhonePickUp right now! Now everything working like I want it from the beginning

    void Update()
    {
    PhonePickUp();
    }
     
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Another thing to note, is this can be simplified to
    if (hit.CompareTag("tagName"))


    As the
    hit
    is already the collider component, and that can compare the tag itself.

    As code reads that:
    ColliderComponent.GetTransformComponent.CompareGameObjectComponent.tag

    technically speaking. Plus it also saves you from having to type more! :cool:
     
    wernetto likes this.