Make an energy wich decreases when the character jumps
I need to make an energy wich decreases when i jump, or when i press space. Practically my character need to fly, so i've made a script that makes him jump too when he is in the air. The only problem is that i don't want he flyes endlessly! Possibly in C#! Oh, and you can tell me also how to make that when the character is on the ground that energy will restore, and maybe how to make a bar that sign how much energy is left? I know it is a really complex question, and understand an as terrible english like mine is difficult, but i hope to find my answer :) Please, is pretty urgent!
Answer by Konomira · Oct 11, 2015 at 08:14 PM
using UnityEngine;
using System.Collections;
public class Volo : MonoBehaviour {
float jumpCount;
public Vector2 jumpForce = new Vector2(0, 300);
bool grounded;
void Update()
{
// If the character has more than 0 jumps left
if (Input. GetKeyDown("space") && jumpCount > 0)
{
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
GetComponent<Rigidbody2D>().AddForce(jumpForce);
jumpCount--; // Reduce the amount of jumps left by 1
}
// If the character is touching the ground
if (grounded)
{
jumpCount = 4; // Reset amount of jumps left to 4
}
}
void OnCollisionEnter(Collision col) // When the character collides with something
{
if(col.collider.tag == "ground") // If the object has the tag "ground"
{
grounded = true;
}
}
void OnCollisionExit(Collision col) // When the character stops colliding with something
{
if(col.collider.tag == "ground") // If the object has the tag "ground"
{
grounded = false;
}
}
}
I've tried that, and it doesen't work.. I'll try to modify my precedent script with some of the things that you have put here, tomorrow. I'll accept the answer, anyway. Thanks anyway :)
Answer by Joppix · Oct 11, 2015 at 06:56 PM
@Konomira ok, This is the script,
using UnityEngine;
using System.Collections;
public class Volo : MonoBehaviour {
public Vector2 jumpForce = new Vector2(0, 300);
float flightSpeed = 10.0f; // Set this speed to whatever you want.
float energyDecay = 1.0f; // Set this to whatever you want.
float energy;
const float maxEnergy = 100;
bool grounded;
void Update ()
{
Vector2 jumpForce = new Vector2(0, 300);
if (Input.GetKeyDown("space") && grounded) // Controls the initial jump
{
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
GetComponent<Rigidbody2D>().AddForce(jumpForce);
}
if (Input.GetKey("space") && energy > 0) // Controls flight for as long as space is held down
{
Vector2 v = GetComponent<Transform>().velocity; // Creates a temporary velocity variable
v.y = flightSpeed; // Sets the temporary variables Y velocity to the flightSpeed
GetComponent<Transform>().velocity = v; // Sets the character velocity to the flightSpeed
energy -= energyDecay; // Reduces energy by energyDecay every frame
}
}
void OnCollisionEnter(Collision col) // When the character collides with something
{
if(col.collider.tag == "ground") // If the object has the tag "ground"
{
energy = maxEnergy; // Reset energy to max
grounded = true;
}
}
void OnCollisionExit(Collision col) // When the character stops colliding with something
{
if(col.collider.tag == "ground") // If the object has the tag "ground"
{
grounded = false;
}
}
}
Now when i apply it to the character, when i press play there is another error:
Assets/scripts/Volo.cs(24,59): error CS1061: Type UnityEngine.Transform' does not contain a definition for
velocity' and no extension method velocity' of type
UnityEngine.Transform' could be found (are you missing a using directive or an assembly reference?)
What should i do? Sorry for all those probems, but i really need it in a few days XD