Player: A sphere with a rigidbody, with a onGround check and a playsound on OnCollisionStay (tag floor), and a material with bounciness,
Level: I have my custom Prefabs (simple geometry) to create a “modular” level, by using plane next to eachother,
The problem: When i run trought 2 platform perfectly near to eachother, i lose for a millisecond the onground boolean and my ball just bounce like there is an obstacle,it also play the sound for OnCollisionExit and OnCollisionStay again.
the problem start at 0.7 second and will repeat on each platform
You can solve this in a couple of ways potentially. First I would suggest moving the sound effect trigger into OnCollisionEnter so you just get it when the ball lands. You will still get events when the ball moves from one plane to another, since they are different objects. You can filter these out either by checking collision.impulse.magnitude against a constant and only play the sound if the impulse is large, which should be when the ball lands. The other way would be to record the time in OnCollisionExit and only play the sound in OnCollisionEnter if the time difference is higher than 0.1 seconds or some other small value.
The first way would look like this:
public float BounceEffectMinImpulse = 1f;
void OnCollisionEnter(Collision collision) {
if (collision.IsTag("Floor") && collision.impulse.magnitude > BounceEffectMinImpulse) {
soundEffect.Play();
}
}
Thank you for the fast response, i have checked your code and adapted to mine, is a great fix for the sound problem! But my problem was not only sound, now when i pass over on another gameobject the bounciness still trigger a minijump (should note be that cause there is no obstacle between those object).
I also have to say that my bolean (OnGround) was not on Collision Enter, but on Collision Stay, cause on enter it sometime will get bugged and unable to jump, i think is still cause from the Material with Bounce);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
private Rigidbody rb;
public float speed;
public Vector3 jump;
public float jumpForce = 2.0f;
public bool isGrounded;
public AudioClip MusicClip;
public AudioClip MusicClip2;
public AudioSource MusicSource;
private void Start()
{
rb = GetComponent<Rigidbody>();
MusicSource.clip = MusicClip;
}
private void OnCollisionEnter()
{
MusicSource.Play();
}
void OnCollisionStay(Collision col) //alla collisione
{
if (col.gameObject.tag == "floor")
{
isGrounded = true;
}
}
void OnCollisionExit() //alla fine della collisione
{
isGrounded = false;
}
/*void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive (false);
}
}*/
private void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); //fisica di movimento
rb.AddForce(movement * speed); //fisica del movimento
if (Input.GetKey(KeyCode.Space) && isGrounded)
{
MusicSource.Play(); //Suona la MusicClip
rb.AddForce(jump * jumpForce, ForceMode.Impulse); //salto
}
}
}
Nope i can confirm that those are perfectly lined up
You can also test this on your macchine Just create 2 object, line It up, ad make ball character move up the intersection
I had a little experiment, and you are right. I found reducing the Fixed Timestep in Project Settings → Time from 0.02 to 0.002 got rid of the problem, you can play around with that value, 0.002 might be lower than necessary.
If you can create the floor as a single mesh, that will also solve it I think too, as there won’t be any seams then. Take a look at generating meshes perhaps: Unity - Manual: Using meshes with C# scripts