• 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 SparkyTheAlbino · Jun 20, 2014 at 01:56 PM · c#rigidbodyboost

Help with boosting a ridgid body

Ive created a script to control movement of a ridgid body and it is working well! Im trying to add features, one of which is when the player holds the space key its speed increases (boost).

Ive implimented some code but there doesnt seem to be any change when I hold space.

My Code:

 public float speed = 10.0f;
 public float maxVelocityChange = 10.0f; // Change this for boost
 public float rotationSpeed = 10.0f;
 public bool allowStrafe;
 public bool allowBoost;
 private bool grounded = false; // Grounding check, could be useful for later
 private float boostVelocityChange;
 private float boostSpeed;
 
 
 void Awake () {
     rigidbody.freezeRotation = true; // Stabalising object on awake
     boostVelocityChange = maxVelocityChange * 2;
     boostSpeed = speed * 2;
 }
 
 void FixedUpdate () {
     if (grounded) {
         // Calculate how fast we should be moving
         Vector3 targetVelocity = new Vector3(0, 0, Input.GetAxis("Vertical"));
         targetVelocity = transform.TransformDirection(targetVelocity); // What it would be if it moved
         if(allowBoost)
         {
             targetVelocity *= speed; // multiply speed to it to account for velocity
         }
         else
         {
             targetVelocity *= boostSpeed;
         }
          
         // rotate
         transform.Rotate(new Vector3(0, Input.GetAxis("Horizontal"), 0) * rotationSpeed);
         
         // Apply a force that attempts to reach target velocity
         
         //Boost code
         //
         //
         if(allowBoost)
         {
             if(Input.GetKey(KeyCode.Space))
             {
                 // Boost Active
                 Vector3 velocity = rigidbody.velocity; // hold velocity
                 Vector3 velocityChange = (targetVelocity - velocity); // hold target
                 velocityChange.x = Mathf.Clamp(velocityChange.x, -boostVelocityChange, boostVelocityChange); // round to fit the max velocity
                 velocityChange.z = Mathf.Clamp(velocityChange.z, -boostVelocityChange, boostVelocityChange);
                 velocityChange.y = 0; // no change in the Y axis
                 rigidbody.AddForce(velocityChange, ForceMode.VelocityChange); // Add the force to the object
             }
             else
             {
                 Vector3 velocity = rigidbody.velocity; // hold velocity
                 Vector3 velocityChange = (targetVelocity - velocity); // hold target
                 velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange); // round to fit the max velocity
                 velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
                 velocityChange.y = 0; // no change in the Y axis
                 rigidbody.AddForce(velocityChange, ForceMode.VelocityChange); // Add the force to the object
             }
         }
         else
         {
             Vector3 velocity = rigidbody.velocity; // hold velocity
             Vector3 velocityChange = (targetVelocity - velocity); // hold target
                velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange); // round to fit the max velocity
                velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
                velocityChange.y = 0; // no change in the Y axis
                rigidbody.AddForce(velocityChange, ForceMode.VelocityChange); // Add the force to the object
         }
         // Strafing code
         //
         //
         if(allowStrafe)
         {
             if(Input.GetKey(KeyCode.Q))
             {
                 Vector3 targetVelocityLeft = new Vector3(-1, 0, 0);
                 targetVelocityLeft = transform.TransformDirection(targetVelocityLeft); // What it would be if it moved
                 targetVelocityLeft *= speed; // multiply speed to it to account for velocity
                 
                 // Apply a force that attempts to reach target velocity
                 Vector3 velocityLeft = rigidbody.velocity; // hold velocity
                 Vector3 velocityChangeLeft = (targetVelocityLeft - velocityLeft); // hold target
                 velocityChangeLeft.x = Mathf.Clamp(velocityChangeLeft.x, -maxVelocityChange, maxVelocityChange); // round to fit the max velocity
                 velocityChangeLeft.z = Mathf.Clamp(velocityChangeLeft.z, -maxVelocityChange, maxVelocityChange);
                 velocityChangeLeft.y = 0; // no change in the Y axis
                 rigidbody.AddForce(velocityChangeLeft, ForceMode.VelocityChange); // Add the force to the object    
             }
             
             if(Input.GetKey(KeyCode.E))
             {
                 Vector3 targetVelocityRight = new Vector3(1, 0, 0);
                 targetVelocityRight = transform.TransformDirection(targetVelocityRight); // What it would be if it moved
                 targetVelocityRight *= speed; // multiply speed to it to account for velocity
                 
                 // Apply a force that attempts to reach target velocity
                 Vector3 velocityRight = rigidbody.velocity; // hold velocity
                 Vector3 velocityChangeRight = (targetVelocityRight - velocityRight); // hold target
                 velocityChangeRight.x = Mathf.Clamp(velocityChangeRight.x, -maxVelocityChange, maxVelocityChange); // round to fit the max velocity
                 velocityChangeRight.z = Mathf.Clamp(velocityChangeRight.z, -maxVelocityChange, maxVelocityChange);
                 velocityChangeRight.y = 0; // no change in the Y axis
                 rigidbody.AddForce(velocityChangeRight, ForceMode.VelocityChange); // Add the force to the object    
             }
         }
     }
 }
 
 void OnCollisionStay () {
     grounded = true;    
 }
Comment
Add comment · Show 2
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 meat5000 ♦ · Jun 20, 2014 at 01:56 PM 0
Share

Try setting allowBoost to true.

It's uninitialised on declaration and I don't see it getting changed anywhere in the script.

avatar image SparkyTheAlbino meat5000 ♦ · Jun 20, 2014 at 02:10 PM 0
Share

Hello, I've set it to true in the inspector and there's no change

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Strange/Buggy code behavier 1 Answer

Accessing RigidbodyFirstPersonController 0 Answers

Rigidbody2D.mass editable during runtime? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges