- Home /
How to calculate the amount of Change of Position.Y for a certain amount of time.
Hello, i have a 2d game that could jump from platform to platform. the problem is I have a Roll mechanism that the player could roll. I want to use that roll when the player's Y position change a lot meaning the player jumped or dropped down on a high platform making him roll down on the ground for a realistic feel. I understand my concept completely but I can't seem to make the code.
P.S 1.) Roll is a boolean so if I wanna use it. Ill only write roll = true;
2.)The script is already on the player so this.gameObject.transform.position.y is usable.
Hi, I would use the velocity of the object. If the velocity is going down and it's magnitude is superior to an amount, then the Roll can be true.
If your object has a rigidbody: use rigidbody.velocity.magnitude to get the "speed" of the object to then compare to a chosen amount/speed. And if rigidbody.velocity.y < 0.0f, this means the object is going down.
If your object doesn't have a rigidbody, you can compute a velocity yourself: store the previous position in a Vector3 (from the previous FixedUpdate or Update) and compute the Vector3 velocity = (currentPosition - oldPosition) / Time.deltaTime;
yup you were right sir. um could i have another question. how do i make it so that the player only rolls once?
Well, it depends on the condition you want to have to allow it to re-roll again, and how you roll it = is it an animation ? Usually it involves a kind of simple state machine = testing and setting Booleans to true/false or/and testing if a certain amount of time has passed...
Set a Boolean to true when you start rolling and set it to false when you want to be able to roll again, maybe when the speed is under a certain amount or when you hit something or start to jump again or all of that :) It's difficult to answer without more details. One general advice is to download the free projects from Unity $$anonymous$$ms on the asset store and check how they do it :)
thanks for the answers, you really were helpful to me. God Bless u sir.
Your answer
Follow this Question
Related Questions
animation["name"].time is always zero? 3 Answers
How to calculate an average frame rate 3 Answers
Calculate in Seconds not Frames? 2 Answers