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 TUNG_LP · Jan 27, 2017 at 06:20 PM · c#rigidbodysprinting

How can I add sprint function into my Script?

The increase speed float is sprint but how can i add the sprint into my fixedupdate??

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MovementController : MonoBehaviour
 {
     public float movementSpeed = 3f;
     public float IncreaseSpeed = 6f;
     public float rotationSpeed = 90f;
 
     public float cameraDistance = 5f;
     public float cameraHeight = 10f;
 
     Transform mainCamera;
     Vector3 cameraOffset;
 
     void Start()
     {
         cameraOffset = new Vector3(0f, cameraHeight, -cameraDistance);
 
         mainCamera = Camera.main.transform;
         MoveCamera();
     }
 
     void  FixedUpdate()
     {
         float rotation = Input.GetAxis("Horizontal");
         float movement = Input.GetAxis("Vertical");
 
         GetComponent<Rigidbody>().velocity = transform.forward * movementSpeed * movement;
 
         transform.Rotate(Vector3.up * rotationSpeed * rotation * Time.deltaTime);
 
         MoveCamera();
     }
 
     void MoveCamera()
     {
         mainCamera.position = transform.position;
         mainCamera.rotation = transform.rotation;
         mainCamera.Translate(cameraOffset);
         mainCamera.LookAt(transform);
     }
 }

Comment
Add comment · Show 4
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 FortisVenaliter · Jan 27, 2017 at 10:02 PM 0
Share

What have you tried so far?

avatar image TUNG_LP FortisVenaliter · Jan 28, 2017 at 02:30 AM 0
Share

everything below this comment

avatar image TUNG_LP FortisVenaliter · Jan 28, 2017 at 02:31 AM 0
Share
  void  FixedUpdate()
      {
  
          float rotation = Input.GetAxis("Horizontal");
          float movement = Input.GetAxis("Vertical");
  
          if (Input.GetButton("IncreaseSpeed"))
          {
              GetComponent<Rigidbody>().velocity = transform.forward * increaseSpeed * movement;
          }
                     
          if (Input.GetButtonUp("IncreaseSpeed"))
          {
              GetComponent<Rigidbody>().velocity = transform.forward * movementSpeed * movement;
          }
                   
          transform.Rotate(Vector3.up * rotationSpeed * rotation * Time.deltaTime);
  
          $$anonymous$$oveCamera();
      }
avatar image TUNG_LP FortisVenaliter · Jan 28, 2017 at 11:16 PM 0
Share

can u help cause it wont work.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by coughlinw97 · Jan 27, 2017 at 10:21 PM

I would just add a while loop using whatever input you want the sprint to be associated with, i like left shift as my sprint, then set the velocity to your sprint speed.

while (Input.GetKey(KeyCode.LeftShift)) { GetComponent().velocity = transform.forward increaseSpeed movement; }

You may also have to add an if statement for setting the speed back to normal if it doesnt automatically

if (Input.GetKeyUp(KeyCode.LeftShift)) { GetComponent().velocity = transform.forward movementSpeed movement; }

Comment
Add comment · Show 4 · 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 TUNG_LP · Jan 27, 2017 at 10:57 PM 0
Share

This caused my game to crash, do i put the if statement within the loop statement so it would look like this. cause this doesnot work for me.

  while (Input.GetButton("IncreaseSpeed"))
         {
             GetComponent<Rigidbody>().velocity = transform.forward * increaseSpeed * movement;
 
             if (!Input.GetButton("IncreaseSpeed"))
             {
                 GetComponent<Rigidbody>().velocity = transform.forward * movementSpeed * movement;
             }
avatar image coughlinw97 TUNG_LP · Jan 27, 2017 at 11:21 PM 0
Share

change the while loop to an if statement, and keep the second if statement outside of the first one. This works for me.

 if (Input.GetButton("IncreaseSpeed"))
                     GetComponent<Rigidbody>().velocity = transform.forward * increaseSpeed * movement;
     
  if (Input.Get$$anonymous$$eyUp("IncreaseSpeed"))
                 GetComponent<Rigidbody>().velocity = transform.forward * movementSpeed * movement;

avatar image TUNG_LP coughlinw97 · Jan 28, 2017 at 12:02 AM 0
Share

the run works but regular movement dont...

  if (Input.GetButton("IncreaseSpeed"))
         {
             GetComponent<Rigidbody>().velocity = transform.forward * increaseSpeed * movement;
         }
                    
         if (Input.GetButtonUp("IncreaseSpeed"))
         {
             GetComponent<Rigidbody>().velocity = transform.forward * movementSpeed * movement;
         }
Show more comments

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I move an object in the direction another object is facing. 1 Answer

Freezing an object and keeping it's velocity 1 Answer

I added Rigidbody to my cube and now it goes super fast when i move! 0 Answers

Question about NavMesh and Rigidbody 0 Answers

How to apply a force at certain point of an object? 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