- Home /
Realistic/No gravity spaceship movement?
Hey, I'm trying to make a spaceship that would have realistic almost Ion-Drive movement, where force is applied to the back upon the press of the W button, but also I need to keep it floating so it defies the normal aspects of a rigidbody. Here's what I have so far. How would I make it with realistic force applied, turnforce, and so it can defy gravity?
var speed = 10;
var turnspeed = 5;
function Update () {
rigidbody.AddRelativeForce (Vector3.up * 1);
if(Input.GetButtonDown("W"))
{
rigidbody.AddRelativeForce (Vector3.forward * 10);;
}
if(Input.GetButtonDown("S"))
{
rigidbody.AddRelativeForce (Vector3.back * 10);;
}
if(Input.GetButtonDown("A"))
{
transform.eulerAngles.y += -turnspeed * Time.deltaTime;
}
if(Input.GetButtonDown("D"))
{
transform.eulerAngles.y += turnspeed * Time.deltaTime;
}
}
Thanks
Have you set gravity to zero? You can also disable gravity on a per-body basis, but if your game is set in space best just to not have any gravity at all. Rigidbodies still work in zero gravity!
Didn't think about setting it to the whole stage, great idea thanks! $$anonymous$$ovement still doesn't work though. And I'm kinda confused as to why.
And for some reason "D" Goes forward, "W" Goes up "S" goes down, "A" goes back. All I'm trying to make is forward/back and a bank of the ship if A/D are clicked.
You need to make sure you're not mixing up local and global coordinates! If you define everything in terms of your rigidbody, it should work better.
Answer by Simon Crowe · Oct 11, 2011 at 03:59 AM
Instead of updating transform.eulerAngles you could use AddRelativeTorque, I imagine this would make it more physically realistic.
You will probably get smother movement if you use Unity's Vertical and Horizontal axes for input instead of detecting when the user presses the W, S, A and D keys. Settings for axes can be accessed though the unity editor drop-down menus via Edit/Project Settings/Input.
In order to achieve zero gravity globally, set Physics.gravity to (0,0,0). Settings for this can be accessed though the drop-down menus via Edit/Project Settings/Physics.
What I've done in the script below is set useGravity on your object's rigidbody to false. This means that other rigidbodies in your scene could be affected by gravity but the rigidbody of the object you attach this script to ignores it.
Here's a very basic script I put together, which you can use as a starting point.
var speed = 0.8; var turnSpeed = 0.15;
function Start () { rigidbody.useGravity = false; }
function FixedUpdate() { if (Input.GetButton("Jump")) { //Spacebar by default will make it move forward rigidbody.AddRelativeForce (Vector3.forward*speed); } rigidbody.AddRelativeTorque ((Input.GetAxis("Vertical"))*turnSpeed,0,0); // W key or the up arrow to turn upwards, S or the down arrow to turn downwards. rigidbody.AddRelativeTorque (0,(Input.GetAxis("Horizontal"))*turnSpeed,0); // A or left arrow to turn left, D or right arrow to turn right. }
That's amazing, But can you possibly tell me why unity is thinking my object is turned 90 degrees, is it because upon exporting I didn't have it facing the correct way? Exported to .FBX from 3ds max.
It is possible that you exported it facing the wrong way. I've never used 3ds max so I don't for sure. All I can suggest is compensating by rotating it 90 degrees in the opposite direction in 3ds max and then exporting and importing again.
I'll try that, thanks for the code. With a little altering this movement will be... Almost flawless.
I'm afraid you thanked me a bit too soon. I've just corrected a major error. I used the Update function ins$$anonymous$$d of the FixedUpdate function when dealing with rigidbodies. That meant that the code would be run every frame rather than every physics step, which could be inefficient and make it framerate dependent.
hi. i use blender and when i import the model to unity and try to move forward it moves upwards. i tried rotating it in blender and then in unity and i tried changing the axis in the input manager. do you know what i can do to fix it.