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
1
Question by blackmethod · Oct 11, 2011 at 03:08 AM · rigidbodygravityforcespaceship

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
Comment
Add comment · Show 4
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 syclamoth · Oct 11, 2011 at 03:15 AM 0
Share

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!

avatar image blackmethod · Oct 11, 2011 at 03:54 AM 0
Share

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.

avatar image blackmethod · Oct 11, 2011 at 03:58 AM 0
Share

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.

avatar image syclamoth · Oct 11, 2011 at 04:16 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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. }

Comment
Add comment · Show 6 · 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 blackmethod · Oct 11, 2011 at 04:05 AM 0
Share

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.

avatar image Simon Crowe · Oct 11, 2011 at 04:10 AM 0
Share

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.

avatar image blackmethod · Oct 11, 2011 at 04:21 AM 0
Share

I'll try that, thanks for the code. With a little altering this movement will be... Almost flawless.

avatar image Simon Crowe · Oct 11, 2011 at 06:05 AM 0
Share

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.

avatar image torogitar · Dec 18, 2013 at 03:01 AM 0
Share

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.

Show more comments

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Artificial Gravity 2 Answers

adding force to an instantiated gameobject in required direction 0 Answers

Gravity is preventing object from moving straight toward its direction when calling rigidbody.AddForce... 1 Answer

Problems making a glider 3 Answers

Gravity doesn't work 2 Answers


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