Mesh colliders going thru each others.

I’m coding a 2d game so no gravity. I have an object which is going through the walls. I googled around and make both the wall and object mesh colliders, static, and convex but no luck.

public float speed = 1;
	public bool on = true;

	void FixedUpdate () {
		if (Input.GetKeyDown("1")) on = true;
		if (Input.GetKeyDown("2") || Input.GetKeyDown("3") || Input.GetKeyDown("4") || Input.GetKeyDown("5")) on = false;
		if (on) {gameObject.transform.Translate( new Vector3( speed * Time.deltaTime * Input.GetAxis( "Horizontal" ), speed * Time.deltaTime * Input.GetAxis( "Vertical" ), 0 ), Space.World );}
	}

I got it to work with move not with simplemove but now my object doesnt actually touch the walls. There is this weird gap between them.

CharacterController controller;
	Vector3 move = Vector3.zero;

	void Start () 
	{
		controller = GetComponent<CharacterController>();
	}
	float speed = 1;
	void Update() 
	{
		move = new Vector3(Input.GetAxis("Horizontal")*speed* Time.deltaTime, Input.GetAxis("Vertical")*speed* Time.deltaTime, 0);
		controller.Move(move);
	}

how did you make 2D objects? in 3D world space? try giving them some volume

–

Its not really 2d. The objects have a z of 0.1

–

3 Answers

3

Using transform.Translate will move the object directly, ignoring any collision… for collision, you most likely should add a CHARACTER CONTROLLER to your player, then use controller.Move or controller.SimpleMove to move it.

(if you prefer to utilize physics, however, you may prefer a rigidbody instead of a character controller)

I copy pasted the code for controller move and am getting "object reference not set to an instance" error.

–

you need to add a character controller to your object that the script is attached to select the object, then on the filebar: Component>Physics>Character Controller

–

I did that and it got rid of the error but not the object isnt moving.

–

notice the sample code uses Input.GetAxis... this uses either wasd or arrow keys.. you may need to adjust the capsule or play with the settings of the controller a bit, (also make sure it's not lodged in any colliders to start) follow the link above and READ IT, the documentation is there for a reason..

–

I made sure its not touching anything to start. And I turned of gravity or else it just falls right off.

–

It sounds like you’re still getting used to the engine, editor and so forth; and maybe google gave you bad advice. Mesh colliders are “easy” to click on for a building model, but are a pain later on. They are officially an advanced feature.

If you like the script you have, just get rid of all the Mesh colliders. For the walls and floor, just make some cubes. They each come with 1 box collider. Once you have them the way you like, can even “check off” the renderer (seeing the cubes will help position them over your floor.)

For the Player, get rid of the MeshCollider (and rigidbody.) Just have the required CharacterController and use the little green pill that you automatically get with it.

If that still isn’t working, it will be way easier to find and ask Q’s, since that’s the most common set-up to start with.

Just checking ... the player should not have a box collider. Just the usual transform, meshRender, meshFilter then plus charController. Am attached box collider "looks like" a solid obstacle, so blocks and forces the player away from it (which would be down if it was more high than low.)

–

That's right. Things got weird with a box collider on my object, so I got rid of it.

–

This script its the closest I got to working but now there is this gap between the wall and player. For some reason they aren’t actually touching.

CharacterController controller;
	Vector3 move = Vector3.zero;

	void Start () 
	{
		controller = GetComponent<CharacterController>();
	}
	float speed = 1;
	void Update() 
	{
		move = new Vector3(Input.GetAxis("Horizontal")*speed* Time.deltaTime, Input.GetAxis("Vertical")*speed* Time.deltaTime, 0);
		controller.Move(move);
	}

as @Owen-Reynolds mentioned above, the character controller uses a capsule as its collider, you can see the capsule when the object is selected, and you can adjust the size of the capsule as needed. This information is all explained in the link I provided in my original answer

–

Something to try, just to get a feel for how physics and colliders work, is to just make a scene with some rigidbodies in it, and some ramps to roll them down and things to hit. Let them fall and roll, Pause, move them, change collider sizes, Play and see. Try to fit a Sphere collider around a model of a cat and see how it rolls. Try again with a capsule collider and watch how that rolls... . Play some 3D games and try to walk into walls, plants and people...they use colliders pretty much the same way Unity does.

–