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 Touchphase.Ended not working in OnCollisionStay? Object not moving back after OnCollisionStay

Discussion in 'Editor & General Support' started by Chii-zy, Jul 14, 2023.

  1. Chii-zy

    Chii-zy

    Joined:
    Jan 27, 2020
    Posts:
    2
    I'm trying to move an object into a collisioned cube based on if the name of the collisioned item has the same name as a displayed textfile and if the wrong object collides (different name) the object gets colored red (all happening in OnCollisionEnter) and should be moved back to its original position (in OnCollisionStay)

    I didn't add the OnCollisionEnter as it wasn't important.

    Outside, in the Update, I inserted this part:
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == UnityEngine.TouchPhase.Ended)
    4.         {
    5.             Debug.Log("still touched + touchphase ended");
    6.             touchEnded = true;
    7.         }
    8.     }
    And OnCollisionStay:
    Code (CSharp):
    1.  private void OnCollisionStay(Collision collision)
    2.     {
    3.         Debug.Log("still touched" + collision.gameObject.name);
    4.  
    5.         if (collision.gameObject.name != quizText.text)
    6.         {
    7.             if (touchEnded == true)
    8.             {
    9.                 ResetTransforms(collision.gameObject);
    10.                 touchEnded = false; // Reset the flag
    11.             }
    12.         }
    13.     }
    And the Reset:
    Code (CSharp):
    1.  void ResetTransforms(GameObject obj)
    2.     {
    3.         if (obj != null)
    4.         {
    5.             obj.transform.localPosition = targetPosition;
    6.         }
    7.     }
    It only outputs Debug.Log("still touched" + collision.gameObject.name);

    If I remove the TouchPhase.Ended, the item jumps between my finger and the original Position, but right now it just stays and nothing happens.

    Tried it first with touchphase directly in the OnCollisionStay but switched to a boolean, it didn't help though.

    Any advice?
    Thanks to anyone looking over this :)
     
    Last edited: Jul 14, 2023
  2. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    Why use OnCollisionStay? If you're trying to do something to a collisioned item at the point of collision, then just do all those things in OnCollisionEnter.
     
  3. Chii-zy

    Chii-zy

    Joined:
    Jan 27, 2020
    Posts:
    2
    If the object is getting moved, you can move it out of the cube without a problem. But if it gets placed there, then you cannot access it anymore, thats why I tried it with OnCollisionStay. I also tried adding waittime with WaitForSeconds but it was still jumping between my finger and the original position. So thats why I tried using OnCollisionStay and touchphase.Ended, but maybe I'm missing some logic here
     
    Last edited: Jul 15, 2023