Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LeungLau · Oct 05, 2017 at 01:22 PM · collisionphysics2d-platformercolliders2d-physics

[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);
     }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

150 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges