- Home /
[2Dplatformer][Problem] When the player closes to the enemy, the enemy pass through the colliders
Hello everyone!! I have a problem when I am creating my game.
I set my enemy object walk left and right periodically and when player is within certain distance between the enemy, the enemy will start state two action to shoot the player and move backwards from the player to keep certain distance.
However, when the player walks into the alert distance the enemy will just drop through the ground or pass through the wall (I used TileToUnity to convert the tilemap created by TileMapEditor, the collider of ground and wall are polygon colliders)
this is the situation looks like when I tested the scene: https://www.youtube.com/watch?v=D6TZaXX1wio
This is the code of enemy:
private Rigidbody2D rb2d; public float maxSpeed; public float minSpeed; public float speed; public float dirChangeDur; public bool stateOne; private float timer; private bool goLeft; private bool goRight; public int health; private bool explode; private Animator anim; public bool stateTwo; private GameObject player; private bool playerOnL; private bool playerOnR; public float alertDistance;
// Use this for initialization void Start () { rb2d = GetComponent(); player = GameObject.FindGameObjectWithTag("Player"); stateOne = true; explode = false; anim = GetComponent(); }
// Update is called once per frame void FixedUpdate () { Debug.Log(health); anim.SetBool("Explode", explode); timer += Time.deltaTime; if (stateOne) { if (timer >= 0) { rb2d.velocity = Vector2.right speed Time.deltaTime; goRight = true; goLeft = false; }
if (timer > dirChangeDur)
{
rb2d.velocity = Vector2.left * speed * Time.deltaTime;
goRight = false;
goLeft = true;
if (timer > dirChangeDur*2)
{
timer = 0;
}
}
if (rb2d.velocity.x > 0)
{
Filp();
}
else
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
}
float disBetweenPlayer = Vector2.Distance(transform.position, player.transform.position);
Vector2 playerPos = player.transform.position;
float playerOnWhere = playerPos.x - transform.position.x;
if (playerOnWhere > 0)
{
playerOnL = false;
playerOnR = true;
}
else if (playerOnWhere < 0)
{
playerOnL = true;
playerOnR = false;
}
if (disBetweenPlayer < alertDistance)
{
stateOne = false;
stateTwo = true;
}
else if (disBetweenPlayer > alertDistance)
{
stateOne = true;
stateTwo = false;
}
if (stateTwo)
{
if (playerOnL && !playerOnR)
{
rb2d.AddForce(Vector2.right * speed * Time.deltaTime);
transform.LookAt(player.transform);
transform.eulerAngles = new Vector3(0, 0, 0);
}
else if (playerOnR && !playerOnL)
{
rb2d.AddForce(Vector2.left * speed * Time.deltaTime);
transform.LookAt(player.transform);
transform.eulerAngles = new Vector3(0, 180, 0);
}
}
if (health <= 0)
{
explode = true;
StartCoroutine(Deactivation(0.5f));
}
}
void Filp() { transform.eulerAngles = new Vector3(0, 180, 0); }
public void TakenDamage(int damage) { health -= damage; }
IEnumerator Deactivation(float seconds) { yield return new WaitForSeconds(seconds); gameObject.SetActive(false); }
And this is the code of player:
public Rigidbody2D rb2d;
public float maxSpeed;
public float speed;
public float maxHeight;
public float jumpForce;
public float fallForce;
private float inputX;
private float inputY;
private bool jumpState;
public bool grounded;
public Vector2 knockPowerR;
public Vector2 knockPowerL;
private Animator playerAnim;
private bool attacking;
private PlayerMeleeAttack attackState;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D>();
playerAnim = GetComponent<Animator>();
jumpState = false;
attackState = GetComponentInChildren<PlayerMeleeAttack>();
}
private void Update()
{
attacking = attackState.attacking;
if (Input.GetKeyDown("space")||Input.GetKeyDown("up"))
{
if (grounded)
{
rb2d.AddForce(Vector2.up * jumpForce);
jumpState = true;
}
}
else if (!Input.GetKeyDown("space") || !Input.GetKeyDown("up"))
{
if (grounded)
{
jumpState = false;
}
}
playerAnim.SetBool("Slash", attacking);
playerAnim.SetBool("Jump", grounded);
}
private void FixedUpdate()
{
inputX = Input.GetAxis("Horizontal");
float walkValue = Mathf.Abs(inputX);
playerAnim.SetFloat("Walk", walkValue);
rb2d.velocity = new Vector2(speed * inputX, rb2d.velocity.y);
if (rb2d.velocity.x > maxSpeed)
{
rb2d.velocity = new Vector2(maxSpeed * inputX, rb2d.velocity.y);
}
if (rb2d.velocity.y > maxHeight)
{
rb2d.velocity = new Vector2(rb2d.velocity.x, maxHeight);
}
if (inputX < 0)
{
Filp();
}
else if (inputX > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
}
public void KnockRight()
{
rb2d.AddForce(knockPowerR * Time.deltaTime);
}
public void KnockLeft()
{
rb2d.AddForce(knockPowerL * Time.deltaTime);
}
void Filp()
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
Your answer
Follow this Question
Related Questions
Why is there no Rigidbody2D.SweepTest() ? 1 Answer
More realistic physics? 0 Answers
BoxCollider get stuck when dragging over the border of two BoxColliders 1 Answer
CircleCollider2D vs PolygonCollider2D: which collision solving is less expensive? 1 Answer
How to check if my enemy hits the ground at a certain velocity then add explosive force. 1 Answer