- Home /
Question by
oscar70701 · Feb 01, 2019 at 01:36 PM ·
c#2d-platformerfallingfixpysics
(at very high altitude many objects are created)How to make the object broke from falling answer
the answer is ; using System.Collections.Generic; using UnityEngine;
public class DW : MonoBehaviour {
public Transform DestroyedVersion;
public float magnitudeCol, radius;
private bool callOnce;
void OnCollisionEnter2D(Collision2D other)
{
if(other.relativeVelocity.magnitude > magnitudeCol && !callOnce)
{
callOnce = true;
Destroy (gameObject);
Instantiate (DestroyedVersion, transform.position, transform.rotation);
}
}
}
Comment
Can you rephrase your question? It's difficult to understand what you're asking.
Also, I think there's some confusion on your part about what these lines are doing:
Instantiate (DestroyedVersion, transform.position, transform.rotation);
DestroyedVersion.localScale = transform.localScale;
Have you tried:
Instantiate (DestroyedVersion, transform.position, transform.rotation);
DestroyImmediate(gameObject);
ins$$anonymous$$d?
Best Answer
Answer by TreyH · Feb 01, 2019 at 03:22 PM
Problem seems to be that your speed is triggering mutliple collisions. Simplest way might be to just mark that you've already hit something.
public float magnitudeCol, radius;
private bool canBreak = true;
void OnCollisionEnter2D(Collision2D collision)
{
if(canBreak && collision.relativeVelocity.magnitude > magnitudeCol)
{
canbreak = false;
Destroy (gameObject);
Instantiate (DestroyedVersion, transform.position, transform.rotation);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612190914im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Type"PathDefinition" does not contain a definition for "GetPathEnumerator" 0 Answers
Flip Player, Not the Player's Child. 3 Answers
Jump Code Not Working (2D C#) 2 Answers