- Home /
Moving colliders that are part of a composite collider
So I've been working on this game for the past few days. It's a 2D puzzle where you have to align multiple cursors in a certain way so they'll press all the buttons on the screen at the same time.
I uploaded a video of what I've come up with, but there are still a lot of things to change. Mainly, one thing that I'm really struggling with is the moving cursors, which are shown 33 seconds into the video. They're not really buggy at all, and they work perfectly for the first few levels, but they don't work at all with the obstacles, which are shown 50 seconds into the video.
This is because the way the cursors (and buttons) move in the game is through setting transform.localPosition instead of using the Unity physics engine, which is what a still cursor does.
I've been trying to get the moving cursors to work through velocity with so far no success. The cursors don't have a Rigidbody. Instead they are children of the main cursor, which has a Rigidbody and a Composite Collider. All the extra cursors (and the main cursor too) have a Box Collider, which is automatically part of the Composite Collider.
Is there any way to get this to work?
EDIT: One idea I had is to save the moving cursor's current position, give the main cursor velocity backwards, reset the moving cursor's position, then give the main cursor the same velocity forwards. This would work perfectly, however the velocity is only applied after a physics update, meaning that I can't all do it in one run. I could make each action happen in a different physics update, but then the cursors would be jittering constantly. Is there perhaps a way to apply velocity immediately?
I don't think you meant transform.localScale but transform, position. You won't get around moving the cursors physically if you want to make use of the collision system. The easiest way to do this is converting the actual mouse movement into the velocity needed. The delta should be a vector2. multiplied by Time.fixedDeltaTime should extend it so as a force it would reach it's destination within one fixed update step. And that force it is you have to assign to all cursors you're controlling. You could just have one inputmanager that knows all cursors and tells them this vector.
Could you write that as a line of code? I'm having trouble figuring out what you mean.
Answer by NewDefectus · Sep 23, 2017 at 04:30 PM
I figured it out eventually by using raycasting to simulate velocity. I created this function:
RaycastHit2D simulatedVelocity(Transform t, Vector2 direction, float distance) {
RaycastHit2D rc = Physics2D.Raycast ((Vector2)t.position, direction, Mathf.Infinity, layerMask);
if (rc.distance > distance)
rc.distance = distance;
return rc;
}
in order to simulate velocity. It's essentially the same as setting the velocity or using AddForce, but you can get the resulting position without having to wait for the physics to update.