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’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Discussion Unity 2022.2.20(DX11) Colliders randomly not working

Discussion in 'Physics' started by karmandroidsingularity, May 29, 2023.

  1. karmandroidsingularity

    karmandroidsingularity

    Joined:
    Nov 26, 2015
    Posts:
    2
    Hi,

    I have a very simple 2D project here.
    There is a ball inside a maze where player will rotate the maze.

    Both of the ball and maze has Rigidbody 2D and Colliders 2D.
    Sometime when the maze rotates, the ball will breakthrough the maze wall (here I say the Colliders is not working).
    Video here:


    I have been trying to:
    1. Tweek the Time in the Project Setting
    2. Using Update(), FixedUpdate(), fixedDeltaTime,deltaTime
    3. Changing the Collision Detection on both ball and maze
    Nothing works. The ball still able to go beyond the wall. What I noticed is that this only happen when either the ball or the maze move/rotate with a high speed.

    This is the code I use to rotate the maze:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MazeManager : MonoBehaviour
    6. {
    7.     public float rotateSpeed = 100.0f; //You will easily see the issue happen when set this to 800 or beyond
    8.     private float playerInput;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.  
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void FixedUpdate()
    17.     {
    18.         playerInput = Input.GetAxis("Horizontal");
    19.         transform.Rotate(Vector3.forward * Time.deltaTime * rotateSpeed * playerInput);
    20.         //transform.Rotate(Vector3.forward * Time.fixedDeltaTime * rotateSpeed * playerInput);
    21.  
    22.     }
    23. }
    This is the code I use to handle the ball movement
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor.UIElements;
    4. using UnityEngine;
    5.  
    6. public class UserInputManager : MonoBehaviour
    7. {
    8.  
    9.     public Rigidbody2D rb;
    10.     public Collider2D col;
    11.     public float jumpAmount = 200.0f;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.      
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         //Can jump only when the Ball touching Maze
    23.         if(col.IsTouchingLayers(LayerMask.GetMask("Maze")))
    24.         {
    25.             if (Input.GetKeyDown(KeyCode.Space))
    26.                 {
    27.                     rb.AddForce(Vector2.up * jumpAmount, ForceMode2D.Impulse);
    28.                 }
    29.         }
    30.      
    31.     }
    32. }
    33.  
    Basically the ball only follow the gravity and able to jump when I pressed Spacebar.

    I have been reading these:
    https://forum.unity.com/threads/colliders-randomly-stops-working.405948/
    https://forum.unity.com/threads/unity5f4-bug-in-charactercontroller.310715/

    But seems both thread is not concluded yet.

    Is there anyone here know the workaround to fix the issue?

    Note: I am not using CC(Character Controller) from Unity.
     
    Last edited: May 29, 2023
  2. karmandroidsingularity

    karmandroidsingularity

    Joined:
    Nov 26, 2015
    Posts:
    2
    Solved this!!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MazeManager : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     public float rotateSpeed = 100.0f;
    9.     [Range(0, 100)]
    10.     public float playerInput = 50;
    11.  
    12.     List<Rigidbody2D> rbList;
    13.  
    14.     void Start()
    15.     {
    16.         var childCount = transform.childCount;
    17.         rbList = new List<Rigidbody2D>();
    18.         for (var childIndex = 0; childIndex < childCount; childIndex++)
    19.             rbList.Add(transform.GetChild(childIndex).GetComponent<Rigidbody2D>());
    20.     }
    21.  
    22.     void FixedUpdate()
    23.     {
    24.         var rotationAngle = Time.deltaTime * rotateSpeed * playerInput;
    25.         var rotationDelta = Quaternion.Euler(Vector3.forward * rotationAngle);
    26.  
    27.         rbList.ForEach(rb =>
    28.         {
    29.             var relativePosition = rb.position - (Vector2)transform.position;
    30.             var rotatedRelativePosition = rotationDelta * relativePosition;
    31.             var newPosition = transform.position + rotatedRelativePosition;
    32.  
    33.             rb.MovePosition(newPosition);
    34.             rb.MoveRotation(rb.rotation + rotationAngle);
    35.         });
    36.     }
    37.  
    38. }
    39.  
    Set the Maze into Kinematic