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 MarioTheDev · Aug 20, 2017 at 11:44 PM · playerelsesprintelse if

Add sprint Key

I want to add a sprint key while you hold Lshift you sprint and when you let go you stop sprinting. I tried with this script and the player will sprint but after you let go of Lshift it will keep the speed at 10. using UnityEngine; using System.Collections;

public class PlayerMove : MonoBehaviour { public float speed = 5f;

 void FixedUpdate () {

     float d = Input.GetAxis ("Horizontal");
     float w = Input.GetAxis ("Vertical");

     Vector3 moveVector = (transform.forward * w) + (transform.right * d);
     moveVector *= speed * Time.deltaTime;

     transform.localPosition += moveVector;

     if (Input.GetKey (KeyCode.LeftShift)) {
         speed = 10f;

         else{
             speed = 5f;
         }
     }
 }

}

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
0
Best Answer

Answer by SneakyLeprechaun · Aug 21, 2017 at 01:24 AM

Hello! Not sure if you copy pasted your code, but there's a bracket missing just before the else, which might be the problem.

Also I'd recommend using the Input.GetButton() function instead of GetKey(), since it allows for a more flexible key input. I don't know, maybe you have your reasons for using GetKey().

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
avatar image
0

Answer by RUTimYourBro · Sep 16, 2017 at 02:22 PM

public class PersController : MonoBehaviour { public float
speed,speedUsePerc,speedMax,sprint,stamina,dash,dashUse,dashKd,sprintUse,staminaKd,staminaRegen,staminaMax;

 private float staminaT = 0,dashT = 0,staminaPercUp,speedPercE;
 public Rigidbody rb;

 private bool kdStaminaT = false;
 private bool kdDashT = false;
 float camRayLength = 100;
 int floorMask; 

 public GUIText StaminaH;
 public GUIText StaminaKD;

 void Awake ()
 {
     floorMask = LayerMask.GetMask ("Floor");
 }

 void Start ()
 {
     rb = GetComponent<Rigidbody> ();
 }

 void Update()
 {
     Move ();
     StaminaH.text = "Stamina " + stamina;
     StaminaKD.text = "" + staminaT;
     speed = Mathf.Clamp (speed, 0, speedMax);
 }

 void FixedUpdate()
 {
     Turning ();
 }
     
     void Turning ()
 {
     Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
     RaycastHit floorHit;
     if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)) 
     {
         Vector3 playerToMouse = floorHit.point - transform.position;
         Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
         rb.MoveRotation (newRotation);
     }
 }

 void Move()
 {
     float moveHorizontal = Input.GetAxis ("Horizontal");
     float moveVertical = Input.GetAxis ("Vertical");

     Vector3 movement = new Vector3 (moveHorizontal,0,moveVertical);

     rb.velocity = movement * speed;


     if (staminaT >= staminaKd) 
     {
         kdStaminaT = false;
         staminaT = 0;
     }
     else if (kdStaminaT == true) 
     {
         staminaT += Time.deltaTime;
     }
     else if(stamina < staminaMax & staminaT >= staminaKd)
     {
         kdStaminaT = true;
     }

     if (dashT >= dashKd) 
     {
         kdDashT = false;
     }

     else if (kdDashT == true) 
     {
         dashT += Time.deltaTime;
     }
          
     if (stamina < staminaMax & movement == Vector3.zero & kdStaminaT == false) 
     {
         stamina += staminaRegen * Time.deltaTime;
     } 

     else if (stamina < staminaMax & movement != Vector3.zero & kdStaminaT == false) 
     {
         stamina += staminaRegen / 2 * Time.deltaTime;
     } 

     else if (speed > speedMax)
     {
         speed = speedMax;
     }

     else if (stamina > staminaMax) 
     {
         stamina = staminaMax;
     }


     if (Input.GetKey (KeyCode.LeftShift) & stamina > 0 & movement != Vector3.zero) 
     {
         rb.velocity = movement * sprint;
         stamina -= sprintUse * Time.deltaTime;
         kdStaminaT = true;
         staminaT = 0;
     }

     if (Input.GetKeyDown (KeyCode.Space) & stamina > 0 & kdDashT == false)
     {
         rb.AddForce(movement * dash,ForceMode.Impulse);
         stamina -= dashUse;
         kdDashT = true;
         dashT = 0;
     }


     if (Input.GetKey (KeyCode.LeftShift) & Input.GetKeyDown (KeyCode.Space) & stamina > 0 & kdDashT == false) 
     {
         rb.AddForce(movement * (dash + speed),ForceMode.Impulse);
         stamina -= dashUse * 0.5f;
         kdDashT = true;
         dashT = 0;
     }
 }

}

First line where all float values dont wont add in the code( That script for a player, them have a sprint,dash,stamina,MOAR timers for cooldowns,stamina regenerate and MOAR MOAR MOAR. Sorry for my clumsy english and i hope that will help you.)))

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

121 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 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 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 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

Flipping Switches 1 Answer

Else and If activated at the same time? 1 Answer

Cursor Lock Code w/ if, else if, and else Statement Errors 1 Answer

IF Else Statement being ignored ,the else if condtion is being ignored and playing first IF condition for all game objects. 0 Answers

Reset values to 0 when no inputs + stupid question about ELSE statement 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