- Home /
Passing through walls. 2d toolkit
Sorry for asking another question about this topic. There seem to be many and from what I've looked through and tried I haven't got the results I've wanted yet.
I've created a few sprites on screen with 2d toolkit. I want one box to act as a wall and be impassable to the player. Instead the player passes right through it. Am I taking the right approach here? Or will this not work at all. I've seen some people talking about using rays and calculating collisions themselves to support this. But this just seems a little overkill and I expected more basic Unity features to handle this.
I've created a script, where I move the player's characterController in Update (If I'm not mistake it belongs here rather than FixedUpdate. FixedUpdate is used if your applying forces to move the rigidbody. Correct me if I am wrong.).
The way I thought to do it was to make the wall sprite: - A static collider (BoxCollider) - no rigidbody
The player has: -a CharacterController(its collider) - a rigidbody with no gravity. isKinematic is unchecked.
using UnityEngine;
using System.Collections;
public class Controls : MonoBehaviour {
public int xSpeed = 32;
public int ySpeed = 32;
protected CharacterController control;
// Use this for initialization
void Start () {
control = GetComponent<CharacterController>();
if (control == null) {
Debug.LogError("No character controller set for '" + name + "'");
}
}
// Update is called once per frame
void Update () {
float xTrans = Input.GetAxisRaw("Horizontal") * xSpeed;
float yTrans = Input.GetAxisRaw("Vertical") * ySpeed;
xTrans *= Time.deltaTime;
yTrans *= Time.deltaTime;
Vector3 move = new Vector3(xTrans, yTrans, 0);
control.Move(move);
}
}
Solved, by default there is no Z depth on sprites that come out of 2d toolkit. You need to turn this on.
Found a "silent film" explaining this
Answer by LeetRooster · Jul 21, 2013 at 04:33 AM
Solved, by default there is no Z depth on sprites that come out of 2d toolkit. You need to turn this on.
Found a "silent film" explaining this
http://www.youtube.com/watch?v=29zU5jz4S8w
However, it turns out you shouldn't use the character controller for a lot of 2d use cases (platformers for example). Since it requires that you also use a capsule collider when you often will want a box collider.
http://forum.unity3d.com/threads/75327-Changing-character-Controller-to-a-box-collider
I did my movements using forces on the rigidbody and managed to keep a box collider working. One other issue that seems to be important is to make sure that you add a good amount of z size to the colliders. I was keeping all my colliers with a z size of 1. With a z size of 1 I was often passing through my walls. I made them a bit larger (32, which gave me sprite cubes of 32x32x32) and they seem to be working better. I'm just wondering if unity isn't super great at handling small box sizes with collisions.