- Home /
Question by
Burnt_Marsupial · Jun 12, 2020 at 10:00 AM ·
scripting beginner
Tracking changes in velocity
I'm trying to make a game where a sudden change in velocity will cause damage or kill a character, but I'm not sure how to go about it.
I tried setting a variable to the rigid body velocity in update and then checking again in late update to find any changes but that doesn't seem to work.
I'm quite new to this so I have no clue where to go from here.
Comment
Answer by wewewu · Jun 13, 2020 at 09:32 AM
This will do it:
[SerializeField] private float maxChange;
private Rigidbody rb;
private Vector3 lastVelocity;
private void Start()
{
rb = GetComponent<Rigidbody>();
lastVelocity = rb.velocity;
}
private void FixedUpdate()
{
Vector3 velocity = rb.velocity;
float dif = Mathf.Abs((velocity - lastVelocity).magnitude) / Time.fixedDeltaTime;
if (dif > maxChange)
{
Damage();
}
lastVelocity = velocity;
}
private void Damage()
{
// Do some damage.
}
Your answer
Follow this Question
Related Questions
Melee & range ai 0 Answers
How to move an airplane in the Z axis? 0 Answers
Getting an error while trying to get scene name 1 Answer
How to make a collision script 1 Answer
GetAxis to GetTouch 0 Answers