Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Cirlce colliders

Discussion in 'Project Tiny' started by brokole, Jan 15, 2019.

  1. brokole

    brokole

    Joined:
    Mar 21, 2018
    Posts:
    11
    View attachment 359623 So its not that easy to explain but basically, I have a ball and that ball can touch 4 colliders called floor, left wall, right wall and ceilling. (look at the picture down bellow)If it isn't touching any of them it falls down except If the ball stays on the floor collider for about a second and I move it afterwards it. Then it thinks it is still colliding with the floor collider and it will float in mid air. does anybody know why this happens.
    P.S. (it also can go through the floor when it is in the float state.)
     

    Attached Files:

  2. brokole

    brokole

    Joined:
    Mar 21, 2018
    Posts:
    11
    my input is as follows:
    if (ut.Runtime.Input.getKey(ut.Core2D.KeyCode.D) && position.x <= move.threshold) {
    direction.x += 1;
    }
    if (ut.Runtime.Input.getKey(ut.Core2D.KeyCode.A) && position.x >= -move.threshold) {
    direction.x -= 1;
    }
    if (ut.Runtime.Input.getKey(ut.Core2D.KeyCode.W) && position.y <= move.threshold) {
    direction.y += 1;
    }
    if (ut.Runtime.Input.getKey(ut.Core2D.KeyCode.S) && position.y >= -move.threshold) {
    direction.y -= 1;
    }
     
  3. valourus123

    valourus123

    Joined:
    Nov 12, 2018
    Posts:
    12
    I wonder...
     
  4. brokole

    brokole

    Joined:
    Mar 21, 2018
    Posts:
    11
    anyone that can kind of help is a hero. I can give more information about the entire script just ask and you will recieve.
     
  5. NiallT

    NiallT

    Joined:
    Sep 22, 2018
    Posts:
    51
    I’m confused. Your code has nothing in it that involves colliders.
     
  6. brokole

    brokole

    Joined:
    Mar 21, 2018
    Posts:
    11
    import _ut_Core2D_Input_GetMouseButtonDown = Module._ut_Core2D_Input_GetMouseButtonDown;

    @ut.executeAfter(ut.Shared.UserCodeStart)
    @ut.executeAfter(ut.HTML.InputHandler)
    @ut.executeBefore(ut.Shared.UserCodeEnd)
    @ut.requiredComponents(game.Move)
    @ut.requiredComponents(ut.Core2D.TransformLocalPosition)
    export class MoveSystem extends ut.ComponentSystem {

    OnUpdate(): void {
    let dt = this.scheduler.deltaTime();

    this.world.forEach([ut.Entity, game.Move, ut.Core2D.TransformLocalPosition],
    (entity, move, transformlocalposition) => {

    /* vectors */
    let direction = new Vector3(0, 0, 0);
    let position = transformlocalposition.position as Vector3;
    let mousePosition = transformlocalposition.position as Vector3;

    /* Touch support */
    let touchEnabled = ut.Core2D.Input.isTouchSupported();
    let touchHappened = false;
    let touchX = -1;
    let touchY = -1;
    let pointerWorldPosition = InputService.getPointerWorldPosition(this.world, this.world.getEntityByName("Camera"));


    /* mouse support */
    // let mouseX = -1;
    // let mouseY = -1; let pointerWorldPosition = InputService.getPointerWorldPosition(this.world, this.world.getEntityByName("Camera"));
    // let pointerDown = ut.Runtime.Input.getMouseButtonDown(0) || (ut.Runtime.Input.touchCount() == 1 && ut.Runtime.Input.getTouch(0).phase == ut.Core2D.TouchState.Began);
    let pointerPressed = ut.Runtime.Input.getMouseButton(0) || (ut.Runtime.Input.touchCount() == 1 &&
    (ut.Runtime.Input.getTouch(0).phase == ut.Core2D.TouchState.Stationary || ut.Runtime.Input.getTouch(0).phase == ut.Core2D.TouchState.Moved));


    /** Touch support */
    if (touchEnabled) {
    if (ut.Core2D.Input.touchCount() > 0) {
    let touch = ut.Core2D.Input.getTouch(0);
    if (touch.phase == ut.Core2D.TouchState.Moved) {
    touchHappened = true;
    let swipeVec = move.touchSwipe;
    let swipeHoz = move.touchSwipe;
    swipeVec.x += touch.deltaX;
    swipeHoz.y += touch.deltaY;
    touchX = swipeVec.x;
    touchY = swipeHoz.y;
    }
    else if (touch.phase == ut.Core2D.TouchState.Ended) {
    touchHappened = true;
    let swipeVec = move.touchSwipe;
    let swipeHoz = move.touchSwipe;
    swipeVec.x += touch.deltaX;
    swipeHoz.y += touch.deltaY;
    touchX = swipeVec.x;
    touchY = swipeHoz.y;
    move.touchSwipe = new Vector2(0, 0);
    }
    }
    else {
    move.touchSwipe = new Vector2(0, 0);
    }
    }

    /** touch support */
    if (touchHappened) {
    let threshold = 20;
    let xDom = Math.abs(touchX) > Math.abs(touchY);
    let yDom = Math.abs(touchY) > Math.abs(touchX);

    if (touchX > threshold && xDom && position.x <= move.threshold) {
    direction.x = 5;
    }
    if (touchY > threshold && yDom && position.y <= move.threshold) {
    direction.y = 5;
    }
    if (touchX < -threshold && xDom && position.x >= -move.threshold) {
    direction.x -= 5;
    }
    if (touchY < -threshold && yDom && position.y >= -move.threshold) {
    direction.y -= 5;
    }
    if (touchY > threshold && yDom && position.y <= move.threshold && touchX < -threshold && xDom && position.x >= -move.threshold) {
    direction.y = 5; direction.x -= 5;
    }
    if (touchY < -threshold && yDom && position.y >= -move.threshold && touchX > threshold && xDom && position.x <= move.threshold) {
    direction.y -= 5; direction.x = 5;
    }
    }

    /** mouse support */
    if(pointerPressed){
    let treshhold = 1;
    let xDiff = pointerWorldPosition.x;
    let yDiff = pointerWorldPosition.y;
    let xDomin = Math.abs(xDiff) > Math.abs(yDiff);
    let yDomin = Math.abs(yDiff) > Math.abs(xDiff);

    if(xDiff > treshhold && xDomin && mousePosition.x <= move.threshold){
    direction.x = 5;
    }
    if(yDiff > treshhold && yDomin && mousePosition.y <= move.threshold){
    direction.y = 5;
    }
    if(xDiff < -treshhold && xDomin && mousePosition.x >= -move.threshold){
    direction.x -= 5;
    }
    if(yDiff < -treshhold && yDomin && mousePosition.y >= -move.threshold){
    direction.y -= 5;
    }
    if(yDiff > treshhold && yDomin && mousePosition.y <= move.threshold&& xDiff < -treshhold && xDomin && mousePosition.x >= -move.threshold){
    direction.y = 5; direction.x -= 5;
    }
    if(yDiff < -treshhold && yDomin && mousePosition.y >= -move.threshold&& xDiff > treshhold && xDomin && mousePosition.x <= move.threshold){
    direction.y -= 5; direction.x = 5;
    }
    }

    /** debug W,A,S,D support */
    if (ut.Runtime.Input.getKey(ut.Core2D.KeyCode.D) && position.x <= move.threshold) {
    direction.x += 1;
    }
    if (ut.Runtime.Input.getKey(ut.Core2D.KeyCode.A) && position.x >= -move.threshold) {
    direction.x -= 1;
    }
    if (ut.Runtime.Input.getKey(ut.Core2D.KeyCode.W) && position.y <= move.threshold) {
    direction.y += 1;
    }
    if (ut.Runtime.Input.getKey(ut.Core2D.KeyCode.S) && position.y >= -move.threshold) {
    direction.y -= 1;
    }

    direction.normalize();
    direction.multiplyScalar(move.speed * dt);

    position.add(direction);
    transformlocalposition.position = position;

    });
    }

    }
    }

    this is everythign at the moment (iam kind off debugging) the problem is that i do 0 with collision or colliders they just some times do work and sometimes dont the same with gravity
     
    Last edited: Jan 21, 2019
  7. brokole

    brokole

    Joined:
    Mar 21, 2018
    Posts:
    11
    the problem lies within the fact that i dont tell the colliders anything but they randomly turn collision on and off. I checked the component when it was floating and found out it somehow thinks it is in collision with the last collider it collided with if it collides for more then 1 second. and it wont reset its own build in collision until I hit a rondom other collider. and this only works 50% of the time sometimes it also goes trough the other colliders and also rembers them as currently colliding with.
     
  8. brokole

    brokole

    Joined:
    Mar 21, 2018
    Posts:
    11
    a visual reprensentation.
     

    Attached Files:

  9. Nkon

    Nkon

    Unity Technologies

    Joined:
    Jun 12, 2017
    Posts:
    65
    Hi there,

    I managed to reproduce the issue you're facing. Looks like the Rigidbody2D is in 'sleep mode' after coming to a halt after a collision. Changing the position of the object didn't wake it up.

    At this point, I would recommend to try a workaround by adding a tiny impulse force at the same time when moving the object. This will force the Rigidbody2D to update itself.

    You can do something like this when the object is moved:
    Code (JavaScript):
    1. let impulse = this.world.getOrAddComponentData(entity, ut.Physics2D.AddImpulse2D);
    2. impulse.impulse = new Vector2(0, .0001);   // just use a very tiny force here
    3. this.world.setComponentData(entity, impulse);
    At least in my test, that caused the floating object to start falling again after moving it.

    If you move the object with physics forces only (by adding AddImpulse2D / SetVelocity2D components), the collisions should work better.

    Cheers!
     
    brokole likes this.
  10. brokole

    brokole

    Joined:
    Mar 21, 2018
    Posts:
    11
    you are a hero thank you.