I have been having issues with box colliders, and it’s probably a me problem because I am new to unity. My character game object will pass. through other game objects (sometimes not always) even through the 2D box collider. I added script to the character that if on contact to set the velocity to zero, which helped quite a bit but doesn’t always do the trick.
The other issue is I will have a dynamic game object pass a static game object and they will sometimes stick together, even if the box colliders don’t touch, although they will be quite close in proximity. This issue has had me stumped, any feedback helps, thanks.
With only a description, it’s impossible to tell you. All I can say is that you should never ever modify the Transform as that’s bypassing physics. The only thing that moves in 2D physics is the Rigidbody2D so you use its API to cause movement i.e. add forces, set velocity etc. Also, it has Discrete and Continuous collision detection modes; plenty of information only about how to use those and what they do.
Maybe a stupid question but you did say you were new to Unity - does your player definitely have a collider so that it can actually collide with other colliders?
Here’s the script that goes with my character. If I set him to low velocity he’ll stop on contact, unfortunately I need a higher velocity for the game to function properly.
As I already said above, you absolutely should not ever modify the Transform when using 2D physics. The Rigidbody2D is in charge of the position/rotation and it writes to the Transform so you’re simply stomping over physics and teleporting from position to position. This won’t give you continuous collision detection as it’s not actually moving with velocity (look at the “Rigidbody2D > Info” foldout, it’ll always have zero velocity). Also, you’re doing this per-frame and physics (by default) doesn’t run per-frame either although that can be changed in settings.
You use the Rigidbody2D API provided to effect movement i.e. forces, velocity, MovePosition/MoveRotation. There’s plenty of tutorials online showing how to do basic movement.
Also, why have you set such a tiny mass? That’s not going to help anything really.
I instead of altering position of rigid body, I changed the velocity and everything cleared up, thanks!
*** if anyone in the future has this problem I have left the change I made to a certain portion of my code. Doing it this way is primitive and should be made more simple but it shows where the issue was. Worked for me at least.
public class CharacterScript : MonoBehaviour {
[SerializeField] private SwipeListener swipeListener;
[SerializeField] private Transform playerTransform;
[SerializeField] private float playerSpeed;
public LogicScript logic;
private Vector2 playerDirection = Vector2.zero;
public Rigidbody2D rb;
private void OnEnable()
{
swipeListener.OnSwipe.AddListener (OnSwipe);
//rb = GetComponent<Rigidbody>();
}
private void OnSwipe(string swipe) {
logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
logic.addScore();
switch (swipe) {
case "Left":
playerDirection = Vector2.left;
rb.velocity = new Vector2(-35,0);
break;
case "Right":
playerDirection = Vector2.right;
rb.velocity = new Vector2(35, 0);
break;
case "Up":
playerDirection = Vector2.up;
rb.velocity = new Vector2(0, 35);
break;
case "Down":
playerDirection = Vector2.down;
rb.velocity = new Vector2(0, -35);
break;
}