- Home /
Question by
SirMarcello · Jun 15, 2020 at 09:36 AM ·
sprite
How to kill a bird if it falling too fast?
Hi. I programming a game in unity. I have bird sprite which is moving like flappy bird. I have collison with the ground.
What i want to do. If the bird falling to fast to the ground. it is die and the game is over. If the bird not too fast, the game is not over.
How can i do that?
Comment
Best Answer
Answer by wewewu · Jun 15, 2020 at 01:20 PM
This might help:
[SerializeField] private float maxChange;
[SerializeField] private float maxVelocity;
private Rigidbody rb;
private Vector3 lastVelocity;
private void Start()
{
rb = GetComponent<Rigidbody>();
lastVelocity = rb.velocity;
}
private void FixedUpdate()
{
// If you want to check velocity change
Vector3 velocity = rb.velocity;
float dif = Mathf.Abs((velocity - lastVelocity).magnitude) / Time.fixedDeltaTime;
if (dif > maxChange)
{
Damage();
}
lastVelocity = velocity;
// If you want to check velocity.
if (rb.velocity.magnitude > maxVelocity)
{
Damage();
}
}
private void Damage()
{
// Do some damage.
}
For example, if you are falling, your velocity will increase by 9.81, but your velocity change will always be 9.81, so choose what you want.
Your answer