0
votes

I have a game where map/background is made up of prefabs. My player and prefabs both have rigidbodies and colliders. Neither of them have is trigger checked and the prefabs have collision detection set as continuous dynamic. The player's collision detection is set on continuous. Each of the prefabs have a mesh collider and the individual walls of the prefabs have box colliders (none are set to is trigger). I keep trying to test it on my phone using Unity Remote 5, and every time I move the player it goes through the walls. If anyone has any advice on how to prevent my player from going through the walls, I would really appreciate it!

My movement script is:

  public class Movement : MonoBehaviour
   {
     private Touch touch; 
     private float speedModifier;

 
void Start()
{
    speedModifier = 0.01f; 
}

void Update()
{
    if(Input.touchCount > 0) 
    { 
        touch = Input.GetTouch(0);
        if(touch.phase == TouchPhase.Moved)
            { transform.position = new Vector3(transform.position.x + touch.deltaPosition.x * speedModifier,
             transform.position.y, 
             transform.position.z + touch.deltaPosition.y * speedModifier);
             }
    }
        
}

}

3

3 Answers

0
votes

It would really help to see your movement script however it sounds like you are moving player by manipulating transform.position or using rigidbody.MovePosition() without setting isKinematic to true. The documentation says;

If the rigidbody has isKinematic set to false, it works like transform.position=newPosition and teleports the object to the new position (rather than performing a smooth transition).

This behaviour will ignore the collision. Also you don't need rigidbody on every GameObject in the scene. 1 rigidbody on the player is enough to collide it with other objects.

0
votes

depend on what I got from your question, when you are playing and the player hit the wall, it goes inside; so the problem might be the mass of the wall. if you must add a Rigidbody to the wall so you need to add more mass to the wall also edit the border of the box collider and make them reasonably a bit wider, otherwise if you dont need a rigidbody on the wall simply keep just the box collider and it will works good. hope this answer help you, and it will be better if you can explain more the situation using some pics.

0
votes

Using the Rigidbody position or applying force tends to cause the Rigidbody to clip through other colliders. I recommend changing rigidbody.velocity instead of directly changing the position.