- Home /
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);
}
}
}
}
}
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);
thank you very very much that worked, any idea on how to add a speed limit?
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
![](https://koobas.hobune.stream/wayback/20220613131953im_/https://answers.unity.com/themes/thub/images/avi.jpg)