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 that I’m trying to implement with the character that will emulate the “wall”.
It literally passes through as if the collider weren’t present.
@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
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.
@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:
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
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:
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…
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);
}
}