2D Collider Help

Hi guys!
I’m working on a game that is part of a project for school. I’ve been trying to set up box colliders to set up “walls” for the game. (it’s a 2D top-down game, by the way) I’ve looked for tutorials all around the Internet and tried to find a solution regarding to making it work the way I want. Here’s some pictures of what I’m trying to accomplish.


This is the 2D box collider for the “wall” I’m trying to create.
This is the 2D box collider that I’m trying to implement with the character that will emulate the “wall”.
2949241--218615--Prob.gif
It literally passes through as if the collider weren’t present.

Please help!
Andrew

How are you moving your player ? And disable “is Trigger” on the wall

@vakabaka I’m moving the player by using a script while inputting WASD into the keypress, using Vector3.(up, down, left, right) for each key press respectively . Also, I disabled the “is Trigger” on the wall, still hasn’t changed anything with the result.

There are different possibility for moving. You can move the gameobject through its transform changes (like transform.translate) and the colliders will be ignored in that case (Unity calculate physics in fixed timesteps, but transform can be changed every frame). Or you can move the gameobject through its rigidbody. This should be more correct, if you will use colliders. Check, how are you moving the player :wink:

And check rigidbody on player. It should not be “kinematic”

So, I’ve set up my movement with only the Rigidbody2D, nothing else. So I’m not quite sure what you mean by transform.translate, other than logically it must mean that it moves an object along the x,y,z axis. I’ve had the body set up with kinematic and it’s all set. I’m still not sure how it’s not registering the colliders, although if it’s set up this way.

Ok, I think my english makes all difficult :slight_smile:

At first, disable kinematic. Kinematic is for moving over transform. or animation.

And show you moving script.

This is moving with translate. It is wrong for you,

This is moving with physics,

or

@vakabaka Alright. So, I might’ve confused myself at first when you mentioned Kinematic. I meant that the rigidbody is not dynamic or static, in the drop down menu for the properties. And, for moving, here’s my script:

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour
{
    private SpriteRenderer spriteRenderer;
    public Sprite Character_Back, Character_Front, Character_Left, Character_Right;
    public float speed = 1.5f;

    Animator animator;

    void Start ()
    {
        animator = GetComponent<Animator>();
        spriteRenderer = GetComponent<SpriteRenderer>();
        if (spriteRenderer.sprite == null)
            spriteRenderer.sprite = Character_Front;
    }

    void Update ()
    {
        if (Input.GetKey (KeyCode.A))
        {
            transform.position += Vector3.left * speed * Time.deltaTime;
            animator.SetFloat ("X-Speed", -speed);
        }
        else if (Input.GetKey (KeyCode.D))
        {
            transform.position += Vector3.right * speed * Time.deltaTime;
            animator.SetFloat ("X-Speed", speed);
        }
        else
        {
            animator.SetFloat ("X-Speed", 0);
        }

        if (Input.GetKey (KeyCode.W))
        {
            transform.position += Vector3.up * speed * Time.deltaTime;
            animator.SetFloat ("Y-Speed", speed);
        }
        else if (Input.GetKey (KeyCode.S))
        {
            transform.position += Vector3.down * speed * Time.deltaTime;
            animator.SetFloat ("Y-Speed", -speed);
        }
        else
        {
            animator.SetFloat ("Y-Speed", 0);
        }
    }
}

I hope it will work :sweat_smile: ( maybe in else the line should be: rb.velocity=newVector2 (0,0);

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour
{
    private SpriteRenderer spriteRenderer;
    public Sprite Character_Back, Character_Front, Character_Left, Character_Right;
    public float speed = 1.5f;

    Animator animator;
    Rigidbody2D rb;

    void Start ()
    {
        animator = GetComponent<Animator>();
        spriteRenderer = GetComponent<SpriteRenderer>();
        if (spriteRenderer.sprite == null)
            spriteRenderer.sprite = Character_Front;
        rb = GetComponent <Rigidbody2D>();
    }

    void FixedUpdate ()
    {
        if (Input.GetKey (KeyCode.A))
        {
            rb.velocity = new Vector2 (-speed, 0);
            animator.SetFloat ("X-Speed", -speed);
        }
        else if (Input.GetKey (KeyCode.D))
        {
            rb.velocity = new Vector2 (speed, 0);
            animator.SetFloat ("X-Speed", speed);
        }
        else
        {
            rb.velocity = new Vector2 (0, rb.velocity.y);
            animator.SetFloat ("X-Speed", 0);
        }

        if (Input.GetKey (KeyCode.W))
        {
            rb.velocity = new Vector2 (0, speed);
            animator.SetFloat ("Y-Speed", speed);
        }
        else if (Input.GetKey (KeyCode.S))
        {
            rb.velocity = new Vector2 (0, -speed);
            animator.SetFloat ("Y-Speed", -speed);
        }
        else
        {
            rb.velocity = new Vector2 (rb.velocity.x, 0);
            animator.SetFloat ("Y-Speed", 0);
        }
    }
}

and set interpolate to interpolate in rigidbody2D.

Okay, wait. I think I just asked about help with my problem with my colliders. Where is the movement script at all involved with this? I’m using Vector3, and it’s been working out fine because it’s part of the RigidBody2D portion of Unity. So, it should be working. I doubt the velocity should be used in this specific case?

you can change all Vector2 (x, y) to Vector3 (x, y, 0) if you don’t like them :frowning:

As said, you can not use transform.position and hope, that your colliders will work. Transform.position will just change the position of the gameobject in the world. This is basically a teleport to the position. Vector3 is not exactly part of Rigidbody2D. In this case, this is one point (position) in the gameworld.
Your line: transform.position += Vector3.left * speed * Time.deltaTime;
do next: new player position (x,y,z) should be = old player position (x,y,z) + vector3 (-1, 0,0) * (how far) * (fps /correction). This is teleport somewhere left from player.
And you can disable Rigidbody2D and Collider2D on the player, the player will still move without them.

Oh, okay. So, I did end up changing everything to your example. I do get this, although: 2958109--219517--prob.gif
It’s pretty much like my player is going on ice. So, I’m not quite sure how to resolve this, even though it also doesn’t go through the colliders still…

Something is wrong with else if statements :slight_smile:

you can use Input.GetAxis,

I like InputManager more as using if-else statements. It is more clear. And it is more flexible with keys setting (check edit - project settings - input, you can see there wich keys are used as default).

Can you check this (it can be little “ice moving”, you can avoid this with lower gravity in InputManager setting for the two axis)?

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour
{
    private SpriteRenderer spriteRenderer;
    public Sprite Character_Back, Character_Front, Character_Left, Character_Right;
    public float speed = 1.5f;

    Animator animator;
    Rigidbody2D rb;

    void Start ()
    {
        animator = GetComponent<Animator>();
        spriteRenderer = GetComponent<SpriteRenderer>();
        if (spriteRenderer.sprite == null)
            spriteRenderer.sprite = Character_Front;
        rb = GetComponent <Rigidbody2D>();
    }

    void FixedUpdate ()
    {
        float x = Input.GetAxis ("Horizontal");
        float y = Input.GetAxis ("Vertical");
        animator.SetFloat ("X-Speed", x*speed);
        animator.SetFloat ("Y-Speed", y*speed);
        rb.velocity = new Vector3 (x*speed, y*speed, 0);
    }
}