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 arcanise · Jan 29, 2014 at 07:51 AM · hoverwasdthrust

move a rigidbody with WASD (hovering)

ok to start i have a sphere that i applied this C# script to that makes it hover exactly how i want it to, but for the life of me i cannot get WASD to move it relative to itself W/S should move forward and backward A/D is strafe i dont have any of my fail attempts in the hover code just the hover code itself, thank you for any help

 using UnityEngine;
 using System.Collections;
  
 public class HoverScript : MonoBehaviour 
 {
     public float hoverDistance = 0.75f;
     public float hoverForce = 10.0f;
     private float currentHeight = 0.0f;
     private float hoverForceMultiplier = 0.0f;
     private Vector3 hoverForceApplied = Vector3.zero;
  
     void FixedUpdate() 
     {
         // -- find hover distance and add force to make hover   
         RaycastHit rayHit;
         if (Physics.Raycast(transform.position, Vector3.down, out rayHit))
         {
            currentHeight = rayHit.distance;
            if (currentHeight < (hoverDistance - Time.deltaTime))
            {
              // find percentage of currentHeight below hoverDistance
              hoverForceMultiplier = (hoverDistance - currentHeight) / hoverDistance;
              hoverForceApplied = (Vector3.up * hoverForce * hoverForceMultiplier) + (Vector3.up * 9.8f);
              rigidbody.AddForce(hoverForceApplied);         
            } else {       
              // apply anti-gravity force for half distance above hoverDistance
              if ((currentHeight - hoverDistance - Time.deltaTime) < (hoverDistance / 2))
              {
               hoverForceApplied = (Vector3.up * 9.8f) * ((hoverDistance - (currentHeight - hoverDistance)) / hoverDistance);
               rigidbody.AddForce(hoverForceApplied);
              }
            }
         }
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by gfoot · Jan 29, 2014 at 09:18 AM

Do something like this to add a left-right force:

 rigidbody.AddForce(Input.GetAxis("Horizontal") * transform.right * ForceMagnitude);

And this to add a forwards-backwards force:

 rigidbody.AddForce(Input.GetAxis("Vertical") * transform.forward * ForceMagnitude);
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 arcanise · Jan 29, 2014 at 05:39 PM 0
Share

thank you very very much that worked, any idea on how to add a speed limit?

avatar image gfoot · Jan 29, 2014 at 06:09 PM 0
Share

One way is to increase the rigidbody's drag, but that also increases how quickly you stop when you release the controls.

Another is to check the current velocity and fade the force applied (Force$$anonymous$$agnitude) to zero as the current velocity approaches the maximum speed. You probably want to only do this when the current input is in the direction of the current velocity, so that the player can still slow down or change direction quickly. You could do something like this:

 // Calculate the force we'd like to apply
 Vector3 force = (Input.GetAxis("Horizontal") * transform.right + Input.GetAxis("Vertical") * transform.forward) * Force$$anonymous$$agnitude;

 // Check the current speed, and if it's above a certain level, we'll limit the force
 float speed = rigidbody.velocity.magnitude;
 if (speed > $$anonymous$$axSpeedLo)
 {
     // Calculate the direction of travel - we'll only limit the force in this direction
     Vector3 velocityDir = rigidbody.velocity / speed;

     // Calculate how much of 'force' lies in the direction of travel
     float forceInDir = Vector3.Dot(force, velocityDir);

     // Only limit the force if it's pointing towards the direction of travel - so
     // we only stop the player from increasing speed
     if (forceInDir > 0)
     {
         // How much shall we reduce the force by?  This value ramps up from 0 to 1
         // as the current speed increases from $$anonymous$$axSpeedLo to $$anonymous$$axSpeedHi
         float forceLoss = (speed - $$anonymous$$axSpeedLo) / ($$anonymous$$axSpeedHi - $$anonymous$$axSpeedLo);
         if (forceLossFactor > 1)
             forceLossFactor = 1;

         // Subtract the calculated amount of the force that's in the direction of travel
         force -= forceLossFactor * forceInDir * velocityDir;
     }
 }

 // Apply the force
 rigidbody.AddForce(force);

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

20 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

Related Questions

Best way to handle a Rigidbody with children -> applying force 0 Answers

I can jump, but not walk? (FPS) 0 Answers

Player movement script problem 1 Answer

rotating hovercar to be at the same angle as the terrain/object below it 1 Answer

Viewmodel/camera rotation when moving 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