Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 God_of_Koolaid · Feb 21, 2021 at 11:12 PM · movement scriptreset

Player sprints, but doesn't return to normal speed

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerMovement : MonoBehaviour {

 public CharacterController controller;

 //**** WALKSPEED AND NORMALSPEED NEED TO BE THE SAME
 public float walkSpeed = 12f;
 public float jumpHeight = 100f;
 public float sprintSpeed = 18f;
 public float moveSpeed = 0f;

 public float gravity = -9.81f;

 Vector3 velocity;
 Vector3 moveDirection;

 public Transform groundDetector;
 public float groundDistance = 0.4f;
 public LayerMask groundMask;

 bool isGrounded;

 void Start()
 {
     controller = GetComponent<CharacterController>();
 }


 // Update is called once per frame
 void Update()
 {
     isGrounded = Physics.CheckSphere(groundDetector.position, groundDistance, groundMask);

     float moveX = Input.GetAxis("Horizontal");
     float moveZ = Input.GetAxis("Vertical");

     if (isGrounded && velocity.y < 0)
     {
         velocity.y = -2f;
     }

     if (isGrounded && Input.GetKeyDown(KeyCode.Space))
     {
         velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
     }


     //Sprint if player is holding left-shift and "W" at the same time, should return to walking speed when left-shift is let go.

     moveDirection = new Vector3(moveX, 0, moveZ);
     Debug.Log(moveDirection);

     if (Input.GetKeyDown(KeyCode.LeftShift) && moveZ == 1 && isGrounded)
     {
         walkSpeed = sprintSpeed;
     }

     if(moveZ == -1)
     {
         Debug.Log("Moving backwards");
     }


     Vector3 move = transform.right * moveX + transform.forward * moveZ;

     controller.Move(move * walkSpeed * Time.deltaTime); ;

     velocity.y += gravity * Time.deltaTime;

     controller.Move(velocity * Time.deltaTime);

 }


}

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

3 Replies

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

Answer by Ermiq · Feb 22, 2021 at 10:50 AM

You set walkSpeed to sprintSpeed and never set it back to walkSpeed. Also, you never check if the Shift has been released. So, your code will never get back to walkSpeed.
Also, notice that GetKeyDown() returns true only once in the one exact frame update when the button has been pressed. In any other moment it's always false.
In your case you need to use GetKey() instead. This one will continuously return true in every frame while the user holds the button pressed, and will return false in any other frame when the button is not held down (released).
Also, to not mess things up you'd better use one more variable, to have non-changable walkSpeed and sprintSpeed and add a new currentSpeed as the one that will be assigned with 12f or 18f depending on does the character sprint or not.

 if (Input.GetKey(KeyCode.LeftShift) && moveZ == 1 && isGrounded)
 {
     currentSpeed = sprintSpeed;
 }
 else
 {
     currentSpeed = walkSpeed;
 }
 ...
 controller.Move(move * currentSpeed * Time.deltaTime);
     
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 God_of_Koolaid · Feb 22, 2021 at 10:45 PM 0
Share

It's working now, thanks so much my g.

avatar image
1

Answer by Megaboy238 · Feb 21, 2021 at 11:25 PM

You have set walkSpeed as sprintSpeed overwriting it, i think you were meant to set moveSpeed = sprintSpeed and the in controller.Move(move moveSpeed Time.deltaTime);

and there is a double ; at the end of that line

Comment
Add comment · Show 5 · 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 God_of_Koolaid · Feb 21, 2021 at 11:29 PM 0
Share

I'll try this, thank you

avatar image God_of_Koolaid · Feb 21, 2021 at 11:45 PM 0
Share

I tried changing it to "moveSpeed = sprintSpeed" and also changed the other line to "controller.Move(move moveSpeed Time.deltaTime)", but then another problem occurs where the player doesn't move till I press "W + left-shift", probably because I've set moveSpeed to 0?

avatar image HoweToGaming God_of_Koolaid · Feb 22, 2021 at 12:37 AM 0
Share

try this or something like it

 if (Input.GetKeyDown(KeyCode.LeftShift) && moveZ == 1 && isGrounded)
             {
                 moveSpeed = sprintSpeed;
             }
             else if(moveZ == 1)
             {
                 moveSpeed = walkSpeed;
             }
             else
             {
                 moveSpeed = 0f;
             }

avatar image Megaboy238 HoweToGaming · Feb 22, 2021 at 12:45 AM 0
Share

Yes correct, sorry thought walkSpeed was controlled elsewhere :)

Show more comments
avatar image
0

Answer by rh_galaxy · Feb 21, 2021 at 11:25 PM

You never assign walkSpeed which is a class variable another value after "walkSpeed = sprintSpeed;". So letting go of the KeyCode.LeftShift on the keyboard does nothing, sprintSpeed will stay on.

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 God_of_Koolaid · Feb 21, 2021 at 11:29 PM 0
Share

I'll try this, thank you

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

120 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

Related Questions

Is Reset() only called in the editor? 1 Answer

Object slide like ice without collision 1 Answer

How to use Configurable Joints and DragRigidBody Script with a door 0 Answers

Isometric movement 3 Answers

whay cant i move with this script???? 3 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