Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Joppix · Oct 11, 2015 at 02:48 PM · c#characterjumpflyingenergy

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
         }
     }
  }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joppix · Oct 11, 2015 at 08:31 PM 1
Share

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 :)

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to use 3D colliders and rigidbody on 2D character controller 0 Answers

My 3D character is flying up when I jump 1 Answer

I have problem. 1 Answer

Problem With a Multi-Jump C# script, robot 2d character controller 1 Answer

C# Code won't ket me jump 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges