My goal is to have a platform destroy if it is landed on. Landing on the platform also bounces me up (it is an endless jumper).
The problem is the destroying of the layers isn't consistent. Sometimes the bounce and destroy work as intended, sometimes the platform won't get destroyed (if I bounce on it again it will destroy), other times the platform will destroy and the bounce won't initiate. I am not sure how to make it work every time and why it isn't working properly.
This is the code I am using for the bounce:
public bool platformTouch; //true or false if you are grounded public Transform groundCheck; //Object which will check if we are grounded public bool groundedTouch; public LayerMask ground; //Decide which layers count as grounded. float groundRadius = .2f; //Radius around ground check object will check if grounded public LayerMask platform; //Decide which layers count as grounded. public Rigidbody2D Player; void FixedUpdate() { platformTouch = Physics2D.OverlapCircle(groundCheck.position, groundRadius, platform); groundedTouch = Physics2D.OverlapCircle(groundCheck.position, groundRadius, ground); } Here is the destroy code I am using. If the player's velocity is less than 0 (falling) it should trigger the destroy. There is also a delay in the destroy, so that the bounce triggers.
public Rigidbody2D Player; void Update() { Player = GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody2D>(); } void OnTriggerEnter2D(Collider2D collider) { if (collider.gameObject.tag == "Destroyer") { if (Player.velocity.y <= 0) { StartCoroutine(DestroyPlatforms()); } } } IEnumerator DestroyPlatforms() { yield return new WaitForSeconds(.1f); //waits .1 seconds Destroy(gameObject); //this will work after .1 seconds. //play sound } 2 Answers
Answers 1
I recommend that you increase your collider on the platform and use OnTriggerExit with Destroy(this,0.1) , again I can be wrong. But mb it will help
Answers 2
Physics engines are not deterministic, and it's usually not a good practice to have 2 objects checking for the same collision (one object could trigger and not the other in the same frame) I think that's the problem here.
The collision should only be handled by the platform and call a bounce method on the player in the OnTriggerEnter2D right before calling the StartCoroutine(DestroyPlatforms());
0 comments:
Post a Comment