Playing same sound on multiple object collide on Unity - unity3d

I have a Dice rolling game. I have a 6 dice which is applied random force in random direction in a box. Now, when Dice collide with each other as well as within the box wall the sound need to be produced.
Currently when i add sound to each dice and trigger it when the dice collide, the sound is wired when all of them plays at the same time.
Is there a better way to produce the sound like real when all 6 dice collide with each other and with the walls of the box.

The flange-like effect you hear happens when two identical sounds are played with a very small delay causing their wavelengths to amplify and dampen themselves.
To avoid such effect you have many options:
To just avoid playing same sfx with delays less than inconceivable to user. (you are probably playing each dice hit sfx twice right now)
Use different samples and play them randomly. (if you can't generate new samples try modifying the ones you have by simply changing their pitch by say 3%-10% to have enough different samples)
If second option does not satisfy your need (project size increase) you can use third party plugins such as master audio to create several custom-pitched sounds out of one single sound at run-time.
You can change the pitch in code (at run-time) and make sure two close hits never play with same (or very close) pitch

It's actually pretty difficult to produce realistic collision sound for multiple objects collision.
If you use the same AudioClip for each dice-to-dice or dice-to-box collision event and trigger them on collision event, the end result will simply sound like an echoed version of the AudioClip with various delays.
If you choose to use a variety of collision AudioClips as a pool to choose from, you might produce ok end results as long as you can guarantee there is no two collision sounds with the same AudioClip playing during any given time period.
The best solution probably is to obtain several recordings of the real scenario (dice rolling and colliding in a box), and randomly play one when you are simulating the collision in game. As long as the duration of the AudioClip matches the simulation, it will be relatively hard to spot it's faked.

Related

Smart pool swimming tracking

I'm a competitive swimmer and would like more detailed feedback on my swims.
A regular smartwatch has gyro/accererometer/gps/magnetometer, but has a few issues:
Laps aren't being counted instantly, often has some delay after making a turn
There's quite some deviation in lap time measurements, I would like to be able to more accurately measure my lap times in a swim, not with a 2 seconds deviation
When not moving my arms (e.g. swimming legs only), it doesn't count my lap
When taking breaks it does not detect this fast enough.
These are just limitations from the currently used hardware.
One solution is to use a touchpad, which are often used in competition, but when swimming with multiple people in a crowded lane, this doesn't work well either.
I've thought about a solution for this, and a possible solution would be to have a "base-station" at one end of a pool, and somehow measure the distance between the base-station and the swimmer. If you know exactly how far away a swimmer is in the pool, you can more accurately count laps/measure lap times/detect pauses.
Only problem I'm facing now is, what method can I use to measure the distance from the base-station and the swimmer?
Some insight:
Sometimes the swimmer is above the water, other times they're underwater.
Sound-based localization is tricky since sound moves 4x faster in water than in air
Radio-based localization is tricky since the speed of light in water is 75% that of in air. (and radio waves get attenuated fast in water)
Camera with some IR LED or swimming-cap color detection could work but not very convenient
Accuracy needed: ~0.5m
Placement of sensor: can be around wrist, or on head, or somewhere else
Anyone got any tips?

Unity2D disable player collision with ennemy when taking damage

My player has multiple hearts, and i want to damage him one heart for every collision with an ennemy, but after each collision i want him to be invulnerable for a certain amout of time, and making him able to pass through the ennemy, thought about disabling his BoxCollider2D for a moment but that makes him fall through the ground as i'm using a Rigibody2D, how can i achieve that ?
There is two ways to do this:
1-https://docs.unity3d.com/ScriptReference/Physics.IgnoreCollision.html
2-https://docs.unity3d.com/Manual/LayerBasedCollision.html (You will change layers for a short period of time)

AI Awareness with Unity3D

I am currently developing an AI system in Unity3D and am wondering if anyone could suggest which of the following awareness models are better?
Use Physics.OverlapSphere at a constant rate of about every 2 seconds to check a range for anything of interest.
Use a Sphere Trigger Collider that is attached to the AI, when an object enters this trigger it starts getting monitored.
Mostly worried about performance vs quality, I have a feeling that model 1 is faster until the AI needs to check everything around it for a particular item, in which case model 2 would return quicker as it already has the collection of local interests. However, will model 2 take up more resources as a trigger collider needs to be sending out checks every physics update?
Your 2nd approach is best way to do it, even though,i can easily say that it all depends on your requirements and both have their own uses. (I am "only absolute way of doing things kinda guy")
With Trigger
void OnTriggerEnter(Collider other)
you can only get one collision body out of many that have collided. It is faster if you have large collisions and u can definetly expand and contract the radius of your trigger to almost use it as the same effect as overlapping sphere (but not recommended for guaranteed results). As it detects only one collided body its not processor intensive.
With Overlap sphere approach you can
Collider[] OverlapSphere(Vector3 position, float radius, int layerMask, QueryTriggerInteraction queryTriggerInteraction);
you can get collider[] or multiple collision bodies that have collided. This is processor intensive, if you have 50 objects colliding and performance will drop the time you call upon this function.
Another Approach.
For simplicity and proper design of mechanics, Trigger bodies can solve for any complicated scenario. I must advice you, from my experience it is always the perspective you rely upon to solve your problem is the factor that will limit your thoughts and ultimately your results.
Keeping that in mind, I would like you try this approach...create a method that uses
public static float Distance(Vector3 a, Vector3 b);
to give you a boolean answer to detect your actor. It is a very good approach since only a language primitive is streamed (between 2 actors) and not an object and performance of in-built function is just negligible. You can have each enemy actor register themselves (their reference) in the List (to get all actors of interest) inside main actor actor if they are at desired proximity (so yes your enemy checks whether its close than the main actor and tells the actor that he is the one who is very close).
I have tested this approach with 20 other actors having their own scripts running in ipod gen 5, with almost no performance drop.

Half scenes with static elements, other with spawn

I have 2D game, where half scene with spawn enemies and (for example) other half scene, where I want use static enemies and other elements.
I thought to create sript that after some time (for example 10 seconds), will stop spawn scripts , and run the movement of other elements.
So. Maybe there is a reasonable solution to this problem.
[UPDATE]
I need the most sensible solution of such a problem, I do not mean to do this, but how to make it better.
1) Can make static elements, which will be a certain time, just stand behind the camera, and then move ... or programmatically create static elements, over time, in advance of known locations...Or download the entire stack of elements over time.
2) Or can completely abandon this idea. A striking example is the Subway Surf, there static scenes (layout) are created in random order.
P.s. I hope I have explained my problem
Just learn to use "Invoke", it's extremely simple.
Invoke( "YourOtherRoutine", 10f );
So after ten seconds it will run the other routine. That routine could easily stop one script running, start another script running, or, whatever it is you want to do. There are tens of thousands of examples of Invoke() and InvokeRepeating() on the usual Unity forums, etc.
From your reference to Subway Surf, I assume that you want to generate static elements like the path and static trains in subway surf and non-Static elements like some moving trains. If so then I have a possible solution.
You can create pre-defined sets of elements (let say 20 or 30 set with different combination of elements) and then spawn them randomly one after another. e.g. have a look at the two reference images below.
Now, note that you might see these scenes exactly as in the images multiple times while playing the game, this is because they are pre-created, The developers behind Subway surf have created these paths and saved them as prefabs and then spawn them at different locations during game play.
You might have noticed that sometime the path is the same but the position of trains is different. This can be achieved by further creating spawn points on your path and then at runtime randomly select points on which you want to spawn your static elements.
In many cases when there are more than one gates you can pass through (I am referring to the gate in the second image). the moving trains spawns on the path of the gate you cross. Spawning the moving train can be achieved as mentioned in step two with a movement script attached to it. Regarding the question of how to know on what path to spawn there are two possible ways (that I can think of right know).
You can keep track of your players current lane and then spawn the train on that lane.
You can place separate triggers on each lane and then detect which lane trigger was triggered and then spawn the train on that lane.
For other moving trains just use the method in step 2 to spawn them but with a movement script attached.

What is the right way to simulate a dice rolling in Unity with a pre-determined outcome?

I have made two dice as rigid bodies and now on application of random forces they give random outcomes. How should I make the dice roll to a predefined outcome?
It would probably be easiest to use Unity's built in animation system to accomplish this. Instead of spending so much time on making a library of forces to apply you would only need to do a total of six animations. Then apply the animation which yields then number you want to each die.