Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Player pushing game boundary collider

Discussion in '2D' started by clearrose, Jul 16, 2015.

  1. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    We were attempting to make game boundaries with 2D colliders and the player keeps pushing them as the character moves forward. I've tried serval different solutions, switch collider types (Including the edge collider), rigid body settings on the player and the boundary. I've even tried by script using mathf.clamp on the inputs and the player pushes onward pass the game world.

    Plz, advise we've had this problem for 3 days now.
     
  2. nocokepepsi

    nocokepepsi

    Joined:
    Jun 9, 2015
    Posts:
    6
    Yeah, I'm having the same issue too ! I would like to see a solution.
     
  3. Zk

    Zk

    Joined:
    May 25, 2013
    Posts:
    19
    If you're just creating boundaries to stop the player from going past a certain point, you shouldn't need a RigidBody2D attached. You would just need the collider. If you remove the RigidBody2D, the boundary should just be fixed and shouldn't be affected by any physics.

    Also, if your player is passing through the collider, you might want to try changing the Collision Detection to Continuous on the player (if it isn't already), which can help prevent that.
     
  4. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Thanks for your help Zk, however I tried as you suggested. By removing the rigibody and putting the player's collision detection to continuos. However the player still passed though the collider.
     
    wlwl2 likes this.
  5. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Check that neither have their collider set to trigger. If at least one does then they will pass through each other
     
  6. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    I just tried Extrapolate and Instead of passing though the collider the player got stuck in the pivot of the collider
     
  7. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Nope none of the colliders have "is Trigger" checked.
     
  8. nocokepepsi

    nocokepepsi

    Joined:
    Jun 9, 2015
    Posts:
    6
    I tried it too and didn't work here ether.
     
  9. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    sorry, that's all I could think of. I had no problems using mathf.clamp when I built my 2d shmup. I have previously also used planes to frame the play area & just turned the mesh renderer off.
     
  10. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Thats ok, but I can't use planes because our game is an 2d side scroller (2D and 3d can't interact).
     
  11. Zk

    Zk

    Joined:
    May 25, 2013
    Posts:
    19
    Can you post some screenshots of the inspector window for the player and collider objects? That might make it easier for us to figure out what the problem is.

    Thanks!
     
  12. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Sure, no problem I should have did that in the first place.

    The player settings:



    The Game boundary Settings:

     
  13. Zk

    Zk

    Joined:
    May 25, 2013
    Posts:
    19
    That looks like it should work to me, although that mass seems very low. I just tested this in my game where I set my character's mass to 0.0001. When I did that my player basically teleported around when my movement script used AddForce on the RigidBody2D. My player still collided with some of the larger Box Collider 2D objects that I had, but shot right through the thinner ones. But I assume your movement is tuned to that mass.

    How are you moving the player with that script?
     
  14. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I have used planes in a 2d shmup that used sprites for enemies, player etc & I have seen other posts in the forum where people are making 2d games with a mix of 2d & 3d assets.
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Something is not correct here, perhaps it's in the way you're describing your problem. not sure.

    You say you have boundaries that use static edge colliders but they move when the player contacts them? If that's the case, then it's not the physics system moving them. The only way they can move in that case is if something is modifying the transform.

    Looking at your player object, the Rigidbody2D is kinematic so I presume you're just moving it by modifying the transform. In this case, I guess you're not actually using collision detection but instead performing your own ray-casts to determine how/where you can move. Interpolation/Extrapolation is for when the Rigidbody2D is moving via its own velocity and is not used if you're just warping it from position to position instantaneously.

    Of course, I'm guessing here given the limited information, Maybe you should post your movement/casting code.
     
    blizzy likes this.
  16. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    hmm.. while that's puzzling that it worked for you. But i only had the mass that low as an experiment because i read that if the mass was lower that what it was colliding with it, the player wouldn't go though the game boundary collider. Of course that didn't work at all.
     
  17. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    while, i was going off what i had read somewhere. Can't quite remember where though.I guess at this point anything is worth trying.
     
  18. Zk

    Zk

    Joined:
    May 25, 2013
    Posts:
    19
    Can you post your Movement Test script and any other scripts related to that movement? Like MelvMay said, since your RigidBody2D is kinematic, you must not be using physics to move your player.
     
  19. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Ok, heres the code we are using:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MovementTest : MonoBehaviour {
    5.  
    6.     public Rigidbody2D Skull;
    7.     public Vector2 velocity;
    8.  
    9.     // Update is called once per frame
    10.     void FixedUpdate () {
    11.    
    12.        
    13.         if (Input.GetKey (KeyCode.Mouse0)) {
    14.            
    15.            
    16.             Skull.MovePosition (Skull.position + velocity * Time.fixedDeltaTime);
    17.            
    18.         }
    19.  
    20.         if (Input.GetKey (KeyCode.Mouse1)) {
    21.  
    22.             Skull.MovePosition (Skull.position += Vector2.left * Time.deltaTime);
    23.         }
    24.     }
    25. }
    26.  
     
  20. Zk

    Zk

    Joined:
    May 25, 2013
    Posts:
    19
    Okay, so it looks like using the MovePosition function on a RigidBody2D that is kinematic like yours will ignore collisions.

    Here's a quote from the documentation: "If the rigidbody is kinematic then any collisions won't affect the rigidbody itself and will only affect any other dynamic colliders." From http://docs.unity3d.com/ScriptReference/Rigidbody2D.MovePosition.html

    It still doesn't seem like it would move the Boundary though. It would just pass through it.
     
  21. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Thanks everybody for your help, we finally solved the problem by reseting the comments.
     
  22. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Seems you should be using '=' and not '+=' as well. Presumably you don't want to be trying to change the Rigidbody2D position directly with that as you're using MovePosition to do that.