Search Unity

[PROBLEM] Two Object near Eachother will trigger reset on collision Stay

Discussion in 'Physics' started by Deleted User, Jul 4, 2017.

  1. Deleted User

    Deleted User

    Guest

    Hi, my english is not the best but i try my best:

    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

    Thank you
     
  2. joebain

    joebain

    Joined:
    Oct 24, 2014
    Posts:
    49
    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:


    Code (CSharp):
    1. public float BounceEffectMinImpulse = 1f;
    2.  
    3.     void OnCollisionEnter(Collision collision) {
    4.         if (collision.IsTag("Floor") && collision.impulse.magnitude > BounceEffectMinImpulse) {
    5.             soundEffect.Play();
    6.         }
    7.     }
     
    Deleted User likes this.
  3. Deleted User

    Deleted User

    Guest

    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);
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     private Rigidbody rb;
    8.     public float speed;
    9.     public Vector3 jump;
    10.     public float jumpForce = 2.0f;
    11.     public bool isGrounded;
    12.     public AudioClip MusicClip;
    13.     public AudioClip MusicClip2;
    14.     public AudioSource MusicSource;
    15.  
    16.     private void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody>();
    19.         MusicSource.clip = MusicClip;
    20.     }
    21.     private void OnCollisionEnter()
    22.     {
    23.         MusicSource.Play();
    24.     }
    25.     void OnCollisionStay(Collision col) //alla collisione
    26.     {
    27.         if (col.gameObject.tag == "floor")
    28.         {
    29.             isGrounded = true;
    30.         }
    31.     }
    32.     void OnCollisionExit() //alla fine della collisione
    33.     {
    34.         isGrounded = false;
    35.     }
    36.  
    37.     /*void OnTriggerEnter(Collider other)
    38.     {
    39.         if (other.gameObject.CompareTag("PickUp"))
    40.         {
    41.             other.gameObject.SetActive (false);
    42.         }
    43.     }*/
    44.  
    45.     private void FixedUpdate()
    46.     {
    47.         float moveHorizontal = Input.GetAxis("Horizontal");
    48.         float moveVertical = Input.GetAxis("Vertical");
    49.  
    50.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); //fisica di movimento
    51.         rb.AddForce(movement * speed); //fisica del movimento
    52.  
    53.         if (Input.GetKey(KeyCode.Space) && isGrounded)
    54.         {
    55.             MusicSource.Play(); //Suona la MusicClip
    56.             rb.AddForce(jump * jumpForce, ForceMode.Impulse); //salto
    57.  
    58.         }
    59.     }
    60.    
    61.    
    62. }
     
  4. joebain

    joebain

    Joined:
    Oct 24, 2014
    Posts:
    49
    All I can think is the two planes might not be perfectly lined up? If there is a small gap it could cause a jump.
     
  5. Deleted User

    Deleted User

    Guest

    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
     
  6. joebain

    joebain

    Joined:
    Oct 24, 2014
    Posts:
    49
    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: https://docs.unity3d.com/Manual/GeneratingMeshGeometryProcedurally.html