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 RussianBsack · Aug 03, 2014 at 12:49 AM · movementcharactercontrollermotormovedirection

CharacterController sprint functionality.

So I'm trying to implement a sprint function, but I'm having trouble because i don't know how to set it so if the LSHIFT is pressed the 'sprint' will take over, when its raised, it will return back to the 'speed'. However, i've tried using the ELSE function but it just produces errors. The code for the else function is;

 else {
     if(Input.GetKeyDown(KeyCode.LeftShift)){
         moveDirection *= sprint;
     }
 }

However, that code keeps bringing errors.

This is the code I'm currently using.

using UnityEngine; using System.Collections;

 public class characterMovement : MonoBehaviour {
 
     public float speed = 6.0F;
 
     public float sprint = 10.0F;
 
     public float jumpSpeed = 8.0F;
 
     public float gravity = 20.0F;
 
     private Vector3 moveDirection = Vector3.zero;
 
     void Update() {
 
         CharacterController controller = GetComponent<CharacterController>();
 
         if (controller.isGrounded) {
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
 
             if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
 
             if (Input.GetKeyDown(KeyCode.LeftShift)){
                 moveDirection *= sprint;
 
             }
         }
 
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
     }
 }
Comment
Add comment · Show 3
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 EZhurst · Aug 03, 2014 at 12:56 AM 0
Share

Get$$anonymous$$eyDown only calls once, you would need to use Get$$anonymous$$ey in order for it to continuously sprint.

avatar image RussianBsack · Aug 03, 2014 at 01:21 AM 0
Share

That still doesn't work..

avatar image RussianBsack · Aug 03, 2014 at 01:25 AM 0
Share

Figured it out. Heres the script.

 void Update() {
 
         CharacterController controller = GetComponent<CharacterController>();
 
         if (controller.isGrounded) {
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
 
             if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
 
             if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift)){
                 moveDirection *= speed * 5.0F;
 
             }
         }
 
         moveDirection.y -= gravity * Time.deltaTime;
         controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
     }

1 Reply

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

Answer by T27M · Aug 03, 2014 at 01:27 AM

GetKey is correct, but I think it's because you have

 moveDirection *= speed;

and

 moveDirection *= sprint;

both working against each other. Try modifying it to this:

     if (Input.GetKey(KeyCode.LeftShift)){
         speed = sprint;
     } else {
         speed = 6.0f;
     }
Comment
Add comment · Show 2 · 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 EZhurst · Aug 03, 2014 at 01:42 AM 0
Share

Wow, I missed that. Still, what I said about Get$$anonymous$$eyDown still improved the code.

avatar image RussianBsack · Aug 03, 2014 at 02:49 PM 0
Share

I've tried to use the else function before, but for some reason errors kept popping up. I've changed my script now from the previous one and now all i did was created a public float;

 public float sprint = 5.0F;

and just set the move direction to add, the sprint speed, for example;

 moveDirection *= speed + sprint;
     //you could use * ins$$anonymous$$d of +.




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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Changing walking speed in CharacterController.Move 1 Answer

Rotate character to the moving direction problems? 2 Answers

Character too floaty after jumping and grappling.,Grapple too floaty 0 Answers

Character Controller Movement 0 Answers

Rigid body with moveDirection 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