Home > Software design >  Detect collisions between 2 objects
Detect collisions between 2 objects

Time:04-30

I'm trying to do a little game on mobile using Unity and I've got a problem with the rotation of a maze.

To add context : When your moving your finger on the screen, the maze is rotating on himself. There is a ball in it and you need to make it go on a cell to win the level.

When the maze is rotating too fast, the ball falls down and go through the ground and I don't know how to fix it. I tried to play with the gravity, colliders... This is the same when the ball is jumping (when the maze is going up and down quickly).

For the moment I just reset the ball position when you're falling.

        {
            ball.transform.position = new Vector3(0, 2, 0);
            maze.transform.position = Vector3.zero;
            maze.transform.rotation = Quaternion.identity;
        }

Do you guys have some ideas ? Thanks

enter image description here

CodePudding user response:

I had a similar problem in a tilt maze mini-game I worked on. Ideally implementing jkimishere's solution will work but I assume the maze is moving too fast for the collisions to register properly. You'll need to smooth the maze's rotation with a Lerp. In our case we had pressure plates with a tilt value, so it doesn't directly translate to your mobile use but perhaps give you a nudge in the right direction. We used:

public GameObject maze;

private float _lerpTime;
private bool _isRotating;
private Quaternion _startingRot, _desiredRot;

private void Awake()
{
  _startingRot = maze.transform.localRotation;
} 

private void Update()
{
  //Don't want to move the maze if we don't ask for it
  if(!_isRotating)
    return;
  
  //Lerp the maze's rotation
  _lerpTime = Mathf.Clamp(_lerpTime   Time.deltaTime * 0.5f, 0f, 1f);
  maze.transform.localRotation = Quaternion.Lerp(_startingRot, _desiredRot, _lerpTime);
  
  //Once the maze gets where it needs to be, stop moving it
  if(affectedObject.transform.localRotation.Equals(_desiredRot)
    _isRotating = false;
}

private void ActivateTilt()
{
  //Set the new starting point of the rotation.
  _startingRot = maze.transform.localRotation;
  
  //However you want to calculate the desired rotation here
  
  //Reset our lerp and start rotating again
  _lerpTime = 0f;
  _isRotating = true;
}

This will ease the rotation of your maze over time. So that the ball can adapt to the new collider positions.

CodePudding user response:

In the rigidbody(for the ball), make the collision detection to continuous, and in the rigidbody for the maze(if you have one) set the collision detection to continuous dynamic. Hope this helps!

CodePudding user response:

I think that is unavoidable if you allow the player to move the platform freely. I suggest you restrain the speed at wich the player can tilt the platform. This way, the ball will have more frames to adapt to the new slope

  • Related