Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by NutellaDaddy · Mar 18, 2014 at 03:15 AM · c#jumpdamagefpscontroller

How to make fall damage using this script right here.

I have a script ,but I can't figure out how to make fall damage. I have a variable jump height that I had set to raise by 1 every second you are in the air and if you are in the air for more than one hundred seconds you will fall and take damage and then it will reset the variabe jumpHeight(which handled the height) back to zero ,but the thing is it sets it back to zero before it can deal the damage and I don't know how to fix this? Help please!

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     //SPEED AND SUCH
     public float speed = 6.0f;
     public float strafeSpeed = 5.8f;
     public float crouchSpeed = 3.0f;
     public float jumpSpeed = 8.0f;
     public float gravity = 18.125f;
     public float jumpHeight = 0.0f;
 
     private Vector3 moveDirection = Vector3.zero;
 
     private bool grounded = false;
     static public bool isRunning = false;
     private bool isCrouched = false;
 
     //STAMINA BAR
     public GUIStyle staminaBar;
 
     private float maxStamina = 100.0f;
     private float curStamina = 100.0f;
     private float staminaBarLength;
 
     void Start () 
     {
         staminaBarLength = 85;
     }
     void Update () 
     {
         if (grounded) 
         {
             moveDirection = new Vector3(Input.GetAxis ("Horizontal"),0,Input.GetAxis ("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             if(Input.GetKeyDown (KeyCode.LeftShift))
             {
                 speed = 10.00f;
                 isRunning = true;
             }
             else if(Input.GetKeyUp (KeyCode.LeftShift))
             {
                 speed = 6.0f;
                 isRunning = false;
             }
         }
         if (grounded == true)
         {
             if(Input.GetKey (KeyCode.Space))
             {
                 moveDirection.y = jumpSpeed;
             }
         }
         //FALL DAMAGE
         if (grounded == false) 
         {
             jumpHeight++;
         }
         if (grounded == true && (jumpHeight >= 90 && jumpHeight <= 139)) 
         {
             PlayerHealth.curHealth -= 25;
         } 
         else if (grounded == true) 
         {
             jumpHeight = 0;
         }
         moveDirection.y -= gravity * Time.deltaTime;
             var Controller = GetComponent<CharacterController> ();
             var flags = Controller.Move (moveDirection * Time.deltaTime);
             grounded = (flags & CollisionFlags.CollidedBelow) != 0;
 
         AdjustCurrentStamina (0);
     }
     void OnGUI()
     {
         GUI.BeginGroup (new Rect (355, 476, staminaBarLength, 125), string.Empty, staminaBar);
         GUI.Label (new Rect (16, 7, 45, 110), "Stamina", staminaBar);
         GUI.EndGroup ();
     }
     void AdjustCurrentStamina(int adj)
     {
         
         if (isRunning) 
         {
             curStamina -= Time.deltaTime * 10.0f;
         }
         if (!isRunning) 
         {
             curStamina += Time.deltaTime * 7.5f;
         }
         if (curStamina < 0) 
         {
             curStamina = 0;
         }
         if (curStamina == 0)
         {
             speed = 6.0f;
             if(isRunning == true && curStamina == 0)
             {
                 speed = 6.0f;
             }
         }
         if (curStamina > maxStamina) 
         {
             curStamina = maxStamina;
         }
             if(maxStamina <1)
                 maxStamina = 1;
         
         staminaBarLength =(85) * (curStamina / (float)maxStamina);
     }
 }

The if statements involving jumpHeight are around line 57!

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

1 Reply

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

Answer by robcbryant · Mar 18, 2014 at 06:39 AM

You could combine the first two if(grounded) statements into one if block --it's a bit messy/redundant right now. You're making the same 2 logic calls when you only need to make 1. You could do the same for the 3rd if grounded statement.

Also it will only damage the player if it's falling between 90 and 140 secs not 100+ if the player falls for longer than 140 seconds it won't damage the player.

It's also not 140 seconds--it's 140 iterations of the update loop--which may be where you're getting confused? The loop will probably hit 140 iterations in like 5-10 seconds. Try this instead:

 //Change your jump logic to save a timestamp of the current time
  if (grounded == true)
 {
 if(Input.GetKey (KeyCode.Space)) {
 moveDirection.y = jumpSpeed;
 beginJumpTime = Time.time; //make sure you make a float variable for this
 }
 }
 
 //Now back to your fall damage logic-----
 
 //FALL DAMAGE
 
 //Take this out--you don't even need it--the clock is keeping track of your time for you
 //if (grounded == false)
 //{
 //jumpHeight++;
 //}
 
 //get the current time spent in the air
 float currentAirTime = Time.time - beginJumpTime;
 
 if (grounded == true && (currentAirTime >= 90.0F && currentAirTime <= 139.0F))
 {
 PlayerHealth.curHealth -= 25;
 }
 
 // You also no longer need to reset this counter--it will automatically reset to a new time stamp the next time the player is grounded and presses the jump button
 //else if (grounded == true)
 //{
 //jumpHeight = 0;
 //}
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

21 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

Related Questions

How to make a player health script and enemy damage script that is easily changed? 2 Answers

2D sidescroller combat, how do I start? 3 Answers

Falling Damage 1 Answer

How to make sure that my cube can only jump when in contact with the ground 2 Answers

Path constrained characters. 1 Answer


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