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 nichirob · Feb 23, 2014 at 06:09 PM · c#first person controllersprinting

Why wont the sprinting on this code work?

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof(CharacterController))]
 
 public class FirstPersonController : MonoBehaviour {
     
     public float mouseSensitivity = 5.0f;
     public float jumpSpeed = 20.0f;
     public float upDownRange = 60.0f;
     public float walkSpeed = 5.0f;
     public float sprintSpeed = 10.0f;
     
     float verticalVelocity = 0;
     float verticalRotation = 0;
     float actualSpeed;
 
     
     CharacterController characterController;
     
     
     // Use this for initialization
     void Start () {
         
         Screen.lockCursor = true; // Hides the cursor on playing
         characterController = GetComponent<CharacterController> (); //Retrieves CC 
         
     }
     
     // Update is called once per frame
     void Update () {
         
     
         // ** Movement of Player **
 
         actualSpeed = walkSpeed;
         float forwardSpeed = Input.GetAxis ("Vertical") * actualSpeed; //Movement speed of player - forward movement
         float sideSpeed = Input.GetAxis ("Horizontal") * actualSpeed / 2; //Movement speed of player - side movement
         
                 
         verticalVelocity += Physics.gravity.y * Time.deltaTime; //Applies acceleration p/s of Gravity when falling
         
         // Restricts player from jumping twice in mid-air                                                                                                                                                               ir until player hits ground
         
         if ( characterController.isGrounded && Input.GetButtonDown ("Jump")) {
             verticalVelocity = jumpSpeed;        
         }
 
         //Allows sprinting
 
         if (characterController.isGrounded && Input.GetKeyDown(KeyCode.LeftShift)) {
             actualSpeed = sprintSpeed;
         }
         if (characterController.isGrounded && Input.GetKeyUp(KeyCode.LeftShift)) {
             actualSpeed = walkSpeed;
         }
 
         Vector3 speed = new Vector3 ( sideSpeed, verticalVelocity, forwardSpeed ); //Vector for movement of the player
         
         speed = transform.rotation * speed; // ?
         
         
         characterController.Move ( speed * Time.deltaTime ); //Applies speed of player p/frame
         
     }
     
 }


Why wont this code work? In the //Allows sprinting, i have the if statement to check whether the person has pressed the left shift, and if they have, change the actual speed of the player (5, stated at the beginning of //Movement of player) to the sprint speed (10). I don't know what's wrong D: Help plz.

Comment
Add comment · Show 1
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 Linus · Feb 23, 2014 at 06:24 PM 0
Share

What currently happens to actualSpeed when holding Left Shift? And what happens when releasing?

Try https://docs.unity3d.com/Documentation/ScriptReference/Input.Get$$anonymous$$ey.html ins$$anonymous$$d of $$anonymous$$eyDown.

1 Reply

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

Answer by aurelioprovedo · Feb 23, 2014 at 06:30 PM

You are setting the value of actualSpeed at line 36. After that, you read its value at lines 37 and 38. Later, you change the actualSpeed value at lines 52 and 55, but you never read the value again.

Solution: move the lines 37 and 38 to the line befor 'Vector3 speed = new Vector3 .....'

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 nichirob · Feb 23, 2014 at 07:51 PM 0
Share

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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to moved a fixed amount per key press using first person character controller 1 Answer

How to apply a deadzone to camera for mouse position on Unity's default FPC? 0 Answers

Making a bubble level (not a game but work tool) 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