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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to detect faces at box with collision, one time per "touch"

Discussion in 'Scripting' started by KortonSVK, Mar 2, 2021.

  1. KortonSVK

    KortonSVK

    Joined:
    Mar 2, 2021
    Posts:
    3
    Hi,
    am new to unity and doing very simple thing. Moving with cube (player) at ground. I have script where collision is happening and detecting if i hit "touch" the ground and in this situation i want to play sound.
    problem is: this sound plays only when i touch ground and then next time it plays only when i leave ground at least for a milisecond and then hit ground again. But when i move slowly, just rolling at ground, sound is not playing, because collision is not happening again after each face touch ground. OnCollisionEnter play once, and OnCollisionStay play as many sounds as frames per second. I want to play sound everytime any of 6 faces of cube touch the ground, you understand. any solution pls? thanks a lot

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerCollision : MonoBehaviour
    4. {
    5.     public PlayerMovement movement;
    6.  
    7.     void OnCollisionEnter(Collision collisionInfo)
    8.     {        
    9.         if (collisionInfo.collider.tag == "Ground")
    10.         {
    11.           FindObjectOfType<AudioManager>().Play("GrassStep");
    12.         }    
    13.     }
    14.  
    15. }
     
  2. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    You could start by adding 6 child objects to the main cube. Each is just a thin box collider that's positioned on each face. Take the collider off the main cube. This gives you the ability to play when certain faces hit, to play different sounds for each, and to track which one collided last. Would make a cool dice roll :)
     
  3. KortonSVK

    KortonSVK

    Joined:
    Mar 2, 2021
    Posts:
    3
    man, really appreciate your answer, i think this could lead to successful solution. But, i have absolutely no idea how to do that :) can u somehow at least show me hot to do with one face? it will help a lot. or at least link me how to google what to do :) i am really beginner in C# and, in unity too. just learning all things around. thanks a lot
     
  4. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Sort of like the screenshot is what I was thinking. Note the "faces" are just thin cubes with no renderers, just colliders, and they're children of the main cube, so they follow along with it. They're also slightly smaller than the faces so there's no overlap between them. Each would need a script to tell the parent cube (or wherever you're managing collision actions) that it was hit, so it could just send a message with its name, "face (0)", "face (1)", etc. The same script would be used for all of them, just send the name, or you can make a public int variable and set it for each one in the Inspector, then send that. Lots of ways to do it.

    Note: I haven't tested this yet, but might if I get a bit of time...would be curious to "throw the die" and see how the sound triggering behaves as it bounces along!

    screenshot_cube_6_sided_colliders.PNG
     
    KortonSVK likes this.
  5. KortonSVK

    KortonSVK

    Joined:
    Mar 2, 2021
    Posts:
    3
    yes, understand. now time to play with unity :) thanks for help, i hope i can proceed. and yes, i really need somehow to fix this small faces to cube faces and then make colliders for each of them.
    if you have a time and you are willing to show me how you would approach to type script for position and collision, it would be superperfect :) but i need to try at my own. thanks
     
    Last edited: Mar 10, 2021
    seejayjames likes this.