- Home /
Simplemove controls without the gravity
Hey all! This has been bothering me and took me a while to find out but I am making an aircraft vehicle and I have made the controls so it speeds up using simplemove. However simplemove adds gravity and I need to disable the gravity, is there anyway to do this? this is my script so far: (I have taken out some of the extra features such as handbreak etc...)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ private var conversionMetrics : float = 9.2; var vehicleSpeed : float = 0.0; var topSpeed = 120; var tireTraction : int = 3;
function Update()
{
var controller : CharacterController = GetComponent(CharacterController);
var moveDirection = transform.TransformDirection(Vector3.forward);
var curSpeed = vehicleSpeed * Input.GetAxis("Vertical");
//Rotate code
transform.Rotate(0, Input.GetAxis("Horizontal") * tireTraction, 0);
//~~~~~~~~~~~~~~~~~~~~~~~~~Increase Speed~~~~~~~~~
if (Input.GetKey("w") || Input.GetKey("up"))
{
if (vehicleSpeed < topSpeed)
{
vehicleSpeed += 1 * Time.deltaTime * conversionMetrics;
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Breaking~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (Input.GetKey("s") || Input.GetKey("down"))
{
if (vehicleSpeed > 0)
{
vehicleSpeed -= 5 * Time.deltaTime * conversionMetrics;
if (vehicleSpeed == 3)
{
vehicleSpeed = 0;
}
}
}
if (Input.GetKey("q"))
{
gameObject.transform.position.y += 1 * Time.deltaTime * conversionMetrics;
}
// Move the controller
controller.Move(moveDirection * vehicleSpeed);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I read on-line that to get rid of the gravity I should use move instead, however the only way I can think to keep it moving is by using a boolean for every action. Is there any other way? please and thank you!
Answer by aldonaletto · Oct 16, 2012 at 01:18 PM
The parameter passed to SimpleMove is the velocity vector, while Move requires the 3D displacement. In order to convert velocity to displacement, just multiply velocity by Time.deltaTime: this calculates the distance moved at such velocity since last frame.
You could use the "Vertical" axis to control acceleration, but the different effects of accelerating and breaking must be taken into account, using different multipliers for positive and negative acceleration:
private var conversionMetrics : float = 9.2;
var vehicleSpeed : float = 0.0;
var topSpeed = 120;
var tireTraction : int = 3;
function Update()
{
var controller : CharacterController = GetComponent(CharacterController);
var moveDirection = transform.TransformDirection(Vector3.forward);
//Rotate code - use Time.deltaTime to get stable results:
transform.Rotate(0, Input.GetAxis("Horizontal") * tireTraction * Time.deltaTime, 0);
// get the acceleration from the "Vertical" axis:
var accel = Input.GetAxis("Vertical") * Time.deltaTime * conversionMetrics;
if (accel > 0){
accel *= 1; // forward acceleration
} else {
accel *= 5; // breaking
}
// apply to the vehicle speed, clamping to the limits:
vehicleSpeed = Mathf.Clamp(vehicleSpeed + accel, 0, topSpeed);
// calculate the horizontal velocity vector:
moveDirection *= vehicleSpeed;
if (Input.GetKey("q")) // include vertical up velocity:
{
moveDirection.y = 1 * conversionMetrics;
}
// Convert velocity to distance multiplying by Time.deltaTime
// and Move the character:
controller.Move(moveDirection * Time.deltaTime);
}
Answer by PAEvenson · Oct 16, 2012 at 11:47 AM
You can just remove gravity from Unity. Edit->Project Settings->Physics->Gravity
or go here http://docs.unity3d.com/Documentation/Components/class-PhysicsManager.html
Thank you for your answer! however my game is an open world game with other vehicles so disabling all the gravity renders them not usable.
how about turning off the rigidbody's gravity http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-useGravity.html
I have, I tried different combinations such as Gravity On and $$anonymous$$inematic On, $$anonymous$$inematic off, gravity off, nothing works, but as soon as I switch to $$anonymous$$ove it works but disables my real movement.
@PAEvenson, the CharacterController isn't under physics control: Physics.gravity is completely ignored.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082428im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Applying force to my object has no effect. 1 Answer
Switching from SimpleMove to Move 1 Answer
Can someone help me with this Rigidbody2D bug? 1 Answer
Rigidbody wont stop moving!!!! AGH!! 2 Answers
SimpleMove not working on iOS? 0 Answers