Collider problems?
Hey Unity Forum.
I have this issue with my game where my avatar kinda falls down into the tile object when i jump around for a short time. I am not sure why he does so. When I check "Used by composite" off on the "Tilemap collider 2D" it seems to work decent. But then my enemy is walking through the "foreground" tiles. So it's a lose lose. It's after he jumps he gets stuck and his feet are down into the tiles "through" the colision collider. I also have a double jump problem when though I have specific collider to my avatars feet.
I would like NOT to be able to wall jump/get stuck on the wall. I would like "Used by composite" to be checked and NOT get stuck in the ground.
Thank you so much for helping out! :) appreciate it!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CrossPlatformInput;
public class Player : MonoBehaviour { //Config [SerializeField] float runSpeed = 5f; [SerializeField] float jumpSpeed = 5f; [SerializeField] float climbSpeed = 5f; [SerializeField] Vector2 deathKick = new Vector2(25f, 25f);
//State
bool isAlive = true;
// **cache, available everywhere for a scope point of view
Rigidbody2D myRigidbody;
Animator myAnimator;
CapsuleCollider2D myBodyCollider;
BoxCollider2D myFeet;
float gravityScaleAtStart;
// Message then methods
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
myBodyCollider = GetComponent<CapsuleCollider2D>();
myFeet = GetComponent<BoxCollider2D>();
gravityScaleAtStart = myRigidbody.gravityScale;
}
// Update is called once per frame
void Update() {
if (!isAlive) { return; }
Run();
ClimbLadder();
Jump();
FlipSprite();
Die();
}
private void Run()
{
float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal");
Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, myRigidbody.velocity.y);
myRigidbody.velocity = playerVelocity;
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
myAnimator.SetBool("Running", playerHasHorizontalSpeed);
}
private void ClimbLadder()
{
if (!myFeet.IsTouchingLayers(LayerMask.GetMask("Climbing")))
{
myAnimator.SetBool("Climbing", false);
myRigidbody.gravityScale = gravityScaleAtStart;
return;
}
float controlThrow = CrossPlatformInputManager.GetAxis("Vertical");
Vector2 climbveclocity = new Vector2(myRigidbody.velocity.x, controlThrow * climbSpeed);
myRigidbody.velocity = climbveclocity;
myRigidbody.gravityScale = 0f;
bool playerHasVerticalSpeed = Mathf.Abs(myRigidbody.velocity.y) > Mathf.Epsilon;
myAnimator.SetBool("Climbing", playerHasVerticalSpeed);
}
private void Jump()
{
if (!myFeet.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; }
if (CrossPlatformInputManager.GetButtonDown("Jump"))
{
Vector2 jumpVelocityToAdd = new Vector2(0f, jumpSpeed);
myRigidbody.velocity += jumpVelocityToAdd;
}
}
private void Die()
{
if(myBodyCollider.IsTouchingLayers(LayerMask.GetMask("Enemy", "Hazards")))
{
isAlive = false;
myAnimator.SetTrigger("Dying");
GetComponent<Rigidbody2D>().velocity = deathKick;
}
}
private void FlipSprite()
{
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
if (playerHasHorizontalSpeed)
{
transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x), 1f);
}
}
}
Answer by 0ColdZero0 · Jan 02, 2019 at 04:06 PM
Did you figure it out? I have the same problem with my Player's feet going under the composite collider.
Answer by Favorlock · Nov 20, 2020 at 03:29 AM
Since I came across this I thought I'd go ahead and mention the solution that worked for me. I was having the exact same issue and what resolved this was setting the player's RigidBody2D component to have continuous collision detection. So far I have not been able to reproduce the issue after making this change. Hope this helps anyone else who runs into this issue and comes across this thread in the future.
Your answer
Follow this Question
Related Questions
Player get's stuck on Tilemap Collider 2D 0 Answers
2D Box Collider not Working 1 Answer
Trigger Collinder doesnt work with Input.GetKey 1 Answer
2d Tilemap collidor not working 0 Answers