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 Lars_vS · Apr 30, 2015 at 10:04 PM · movementjoystickkeyboardspaceacceleration

Spaceship movement

Hello,

I'm kinda new to unity and I want to make a space fighter game. I want it to exist in all three axis, meaning that I can pitch (W and S keys), roll (A and D keys) and yaw (Q and E keys). I got that down and it works (besides the part where it stops spinning, some sort of counter rotation if I don't use any buttons), but I'm struggling with the acceleration (R and F keys).

I do not want my ship to return to a standard rotation when I release all keys.

I have tried using AddForce, AddRelativeForce, transform.position and velocity, in C#, but when I use any of these, the ship doesn't move in the direction I'm facing.

For example, if I add 10 force on Start(), my ship moves forward and (since there's no drag) keeps doing so, which is supposed to happen. But as soon as I start to rotate, it keeps moving in the same direction relative to the the world axis, not relative to the space ship's axis.

Is it possible to move the ship in the direction it is facing, without applying a new force? If so, what line of code should I use? And if not, how can I fix the problem?

I want to be able to use a joystick in the future, but I should be able to do that myself.

 void FixedUpdate () {
         targetspeed = Mathf.Round(speed * maxspeed); 
         currentspeed = Mathf.Round(GetComponent<Rigidbody> () .velocity.z);
 
         float roll = Input.GetAxis ("Horizontal"); //A en D
         float pitch = Input.GetAxis ("Vertical"); //W en S
         float yaw = Input.GetAxis ("Roll"); //Q en E
         float throttle = Input.GetAxis ("Throttle"); //R en F
 
         if (Input.GetKey ("r") && speed < 1f) {
 
         }
         if (Input.GetKey ("f") &&  speed > -0.1f) {
 
         }
         
         GetComponent<Rigidbody> ().AddRelativeTorque(pitch*turnspeed*Time.deltaTime, yaw*turnspeed*Time.deltaTime, -roll*turnspeed*Time.deltaTime);
Comment
Add comment · Show 2
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 GregoryNeal · Apr 30, 2015 at 10:12 PM 0
Share

It sounds like it's working fine if you want spaceship motion. If you turn off your thrusters in space then start rotating, you will still be moving in the same direction as when you had your thrusters on. Unless I'm misunderstanding you.

avatar image Lars_vS · May 09, 2015 at 08:16 PM 0
Share

First, sorry for the late reaction. I've been kinda busy lately and didn't get to making games a lot. Anyway, thanks for the answers! Now that someone explaned it I realize that this is too hard to start off with. I'll just continue this project with "arcade physics" and try to make this with horizontal gameplay. I might get back to this later, when I know a bit more :)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by AlwaysSunny · Apr 30, 2015 at 10:09 PM

"Is it possible to move the ship in the direction it is facing, without applying a new force?"

Nope. That would violate Newton's First Law of Motion. ;)

I think you may be confused about what you're asking for. It sounds like what you want is some kind of hybrid between "true" physics and "arcade" physics. This is what developers should want in most cases, but it requires careful consideration and a decent familiarity with the physics system and its many "gotchas".

In "arcade" style phyics, vessels have no momentum (or have high drag). Instead, they instantly set their velocities based on the desired movement direction (e.g. vessel.forward). In that case, you shouldn't be using forces to do anything. Script all your kinematics by hand. Let the physics system handle collisions, but nothing else.

In "true" physics, vessels will continue in a straight line forever until acted on by some force, like thrust. These vessels have momentum and no drag. They can rotate without changing their velocity, then apply a force to change their velocity. They require N seconds of thrust to reach a certain speed, then N seconds of force in the opposite direction to stop. In this case, you can let the physics system do nearly everything for you. All you do is apply forces.

In practice, a true-physics space sim is more aggravating than fun. And a fully "arcade" style physics sim feels clumsy and "retro". Your job is to strike some kind of satisfying balance between the two.

True forces (as opposed to scripted kinematics) can work for a true/arcade hybrid, provided you make several concessions. One key "unrealistic" change involves drag. The perfect balance between drag and thrust ( AddForce / AddTorque ) will give you a fun, responsive craft.

Don't forget about the rules and best practices associated with rigidbodies and physics. Carefully review ALL the documentation associated with physics prior to investing a ton of time on a potentially unworkable model.

Comment
Add comment · 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
0

Answer by BMRX · Apr 30, 2015 at 10:17 PM

So when you try to rotate/turn your ship Ex: Bank left. Your ship will rotate in that direction, yet the force is still pushing you in the same axis?

Ship is heading towards the Y axis, ship banks towards X, ship keeps moving towards Y?

Almost sounds like you've coded throttle to move only in the axis that the ship is moving, if that makes sense. You might want to try adding force not in the axis you want to go but instead the direction you want to go.

I don't know if this would work but maybe it will give you an idea:

 rb.AddForce(transform.TransformDirection(Vector3.right) * throttle);

http://docs.unity3d.com/ScriptReference/Transform.html

Don't take me for an expert, I'm still learning as well.

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 AlwaysSunny · Apr 30, 2015 at 10:58 PM 1
Share

You're correct, the OP is failing to apply force in the ship's forward direction.

 body.AddForce( body.transform.forward * whatever );

Guess I forgot to mention that. He's also misunderstanding various other critical aspects of creating a stable, satisfying rigidbody controller. It is not the straightforward task it appears to be.

avatar image BMRX · Apr 30, 2015 at 11:03 PM 0
Share

Nice, thanks sunny thinking this stuff through is too much fun. Hopefully OP can take your advice and get it to work.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Mobile Virtual Joysticks / Keyboard Script Conversion 0 Answers

Mimic Keyboard Controls with Joystick 0 Answers

Trouble with diagonal acceleration in Quake style movement 0 Answers

Aceleration 2D 0 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