I am trying to destroy just 1 tile at a time on collision in a 2D TileMap. I am using the 2017.2beta and I can debug the collision with the right coordinates, but I am not able to just destroy that 1 tile on contact.
Also I have a weird bug, my walls (blocking layer) working fine on the outer walls but sometimes my character can just move through them, just in some parts of my tilemap.
It is really strange, the collisions are all detected and working. What am I missing here?
Would you share some code samples for how you’re handling the collision/destruction? Be sure to use code tags.
It’s best to just focus on one problem at a time, but for your second point if you see colliders moving through each other, you’re either moving them VERY fast, or you’re not using the proper functions to move your character so that they take physics into account (or your physics layers aren’t set up properly, but since you say it only happens sometimes, that’s probably not the case). Your movement should be handled using Rigidbody only, not Transform.
Ah thank you, moving to rigidbody2d.velocity(x & y) did the trick. Surely I had to hit freeze Rotation Z also and made my Character Dynamic. Collision are now working with all tiles. But my Character lost his 1 tile at a time movement (should be manageable for me to find the solution) and the sound for the footsteps is going nuts now
But anyway, I´m guessing this is the right track. Learned a lot already about vector2 vector3. Appreciate
Here my code for now, just the movement part.
public Vector2 velocityX = new Vector2(0,5);
public Vector2 velocityY = new Vector2(5, 0);
public Rigidbody2D rb2D;
void Start()
{
rb2D = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
if (Input.GetKey(KeyCode.UpArrow))
{
rb2D.MovePosition(rb2D.position + velocityX * Time.fixedDeltaTime);
animator.SetTrigger("isUpDown");
StartCoroutine(PlayFootSteps());
}
else if (Input.GetKey(KeyCode.RightArrow))
{
rb2D.MovePosition(rb2D.position + velocityY * Time.fixedDeltaTime);
mySpriteRenderer.flipX = false;
animator.SetTrigger("isWalking");
StartCoroutine(PlayFootSteps());
}
else if (Input.GetKey(KeyCode.DownArrow))
{
rb2D.MovePosition(rb2D.position - velocityX * Time.fixedDeltaTime);
animator.SetTrigger("isUpDown");
StartCoroutine(PlayFootSteps());
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
rb2D.MovePosition(rb2D.position - velocityY * Time.fixedDeltaTime);
mySpriteRenderer.flipX = true;
animator.SetTrigger("isWalking");
StartCoroutine(PlayFootSteps());
}
else
{
animator.SetTrigger("isIdle");
StopCoroutine(PlayFootSteps());
}
}
In OnCollisionEnter, you get a Collision2D object passed into the function, which contains all the information you need about the collision. Using collision.gameObject you could do a GetComponent() or whatever you need to in order to figure out which specific tile it is.
Your code sample there seems like it should work if your WorldToCell function is returning the proper tile position, is it not?
Also, you can use the function Rigidbody.MovePosition if that’s easier. You should be able to use that in place of a transform.position set, without having to get into setting velocity.
You’re making multiple posts and just saying it does not work. It does work, it’s that you have a problem. Rather than necroing threads, please put some effort into describing what you’re doing as your issue isn’t necessarily related to the thread at all.