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
using UnityEngine;
public class PlayerCollision : MonoBehaviour
{
public PlayerMovement movement;
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Ground")
{
FindObjectOfType<AudioManager>().Play("GrassStep");
}
}
}
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
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
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!
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