- Home /
3d Side Scroller - Cutting of velocity when characters energy hits zero..
I am building a sidescroller game with a dragon that I want to fly. But instead of allowing the player to just constantly fly around the map, the dragon needs to be constrained to energy usage. I have the UI Slider reacting to his flight but when the value hits zero I want velocity to be cut off until the energy bar has enough energy to take flight again. So I have built a separate script to control the flight, energy usage, what happens when the energy hits zero and when he is on the ground to regain that energy slowly. I am pretty sure I am approaching this correctly however the code structure is incorrect and I would very much appreciate if someone can take a look and tell me what I need to change please.
public class Flight : MonoBehaviour
{
public KeyCode jumpButton;
public float jumpForce;
public UnityEngine.UI.Slider slider;
public float energyDecay;
public float energyRestore;
float energy;
const float maxEnergy = 100;
bool grounded;
Rigidbody rb;
void Start()
{
energy = slider.value;
slider.maxValue = maxEnergy;
slider.value = maxEnergy;
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetKey(jumpButton) && energy <= maxEnergy)
{
Jump();
}
else if (energy <= 5f)
{
GetComponent<Rigidbody>().velocity = Vector3.zero;
}
}
private void FixedUpdate()
{
if (grounded)
{
slider.value += Time.deltaTime * energyRestore;
}
}
void Jump()
{
if (Input.GetKey(jumpButton))
{
rb.velocity = Vector3.up;
rb.AddForce(new Vector3(0, jumpForce));
slider.value -= Time.deltaTime * energyDecay;
}
}
void OnCollisionEnter(Collision col)
{
if (col.collider.tag == "Ground")
{
grounded = true;
}
}
void OnCollisionExit(Collision col)
{
if (col.collider.tag == "Ground")
{
grounded = false;
}
}
}
It's bad practice to get value from sliders, it's the slider that should get its value from component/controller.
So me declaring energy equal to the slider's value is whats needs to be changed?
No that was not an answer to your question, it's just best practice.
Answer by AmirSavand · Oct 20, 2017 at 06:02 PM
I'm confused with your jump condition.
In the Update() you check this: if (Input.GetKey(jumpButton) && energy <= maxEnergy)
and in your jump function you check if (Input.GetKey(jumpButton))
.
Why are you checking for key down twice? and why the second time you don't check the energy? Is the Jump function being called from somewhere else too? (Other than Update())
Also, the second condition else if (energy <= 5f)
will never be called since energy is always less than maxEnergy.
If you want to stop player form Jump()ing when energy is not enough you should do this:
if (Input.GetKey(jumpButton) && energy >= requiredEnergyToJump)
requiredEnergyToJump
is the minimum energy for Jump()ing. (you should set that).
OLD ANSWER
If you want to stop an object (velocity) check this answer out: http://answers.unity3d.com/questions/12878/how-do-i-zero-out-the-velocity-of-an-object.html
And here's how to slow down an object (velocity) till it stops: http://answers.unity3d.com/questions/336009/how-do-i-make-an-object-slow-down-to-a-stop.html
lol... ya i am just getting to know the community. As it turns out the code to stop the velocity did exactly as intended and I was able to get it to respond accordingly thank you.
Now it's just down to restoring the energy when i hit anything tagged ground.
@studlets Awesome! Restoring energy would be a piece of cake using TriggerEnter/Exit. BTW don't forget to mark my answer as solution and upvote ;)
So after many hours of playing around with my script I was able to get the results I needed. This Script designated to a single $$anonymous$$ey that you set through the inspector. You can also set the energy decay rate and the energy restore rate. The script will also look for an energy slider that will have to be dropped in the inspector. Thank you @amirm3hdi for re$$anonymous$$ding me to follow best coding practices. It's important to learn best practices as a beginner and I thank you for speaking out. This is the finalized Script that solved all my issues.`
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Flight : $$anonymous$$onoBehaviour {
public $$anonymous$$eyCode jumpButton;
public float jumpForce;
public UnityEngine.UI.Slider slider;
public float energyDecay;
public float energyRestore;
float energy;
const float maxEnergy = 100;
Rigidbody rb;
void Start()
{
energy = maxEnergy;
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.Get$$anonymous$$ey(jumpButton))
{
Jump();
}
}
public void Jump()
{
if (energy >= 15f)
{
rb.velocity = Vector3.up;
rb.AddForce(new Vector3(0, jumpForce));
energy -= energyDecay * Time.deltaTime;
UpdateUIslider();
}
if (energy <= 0f)
{
jumpForce = 0f;
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
}
}
public void Energyregen()
{
energy += energyRestore * Time.deltaTime;
UpdateUIslider();
}
public void UpdateUIslider()
{
slider.maxValue = maxEnergy;
slider.value = energy;
}
void OnCollisionStay(Collision col)
{
if (col.collider.tag == "Ground")
{
Energyregen();
}
}
}`
Your answer
Follow this Question
Related Questions
2D character can't jump off of platform floating in water 0 Answers
Add/remove Gravity with code 1 Answer
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
how can I make a character fly when holding down the jump button? 1 Answer
Rotating emitter direction whilst maintaining particle velocity 1 Answer