Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 GiacomInox · May 01, 2016 at 08:16 AM · physicscontrols

How can I have good acceleration mechanics for a 2d spaceship? (C#)

I'm trying to recreate a ship that moves at 0g, but the AddForce argument doesn't follow the ship orientation. How can I get the "booster" effect where I can accelerate and decelerate with the vertical controls and the thrust is always behind the ship?

Here is the code I used: using UnityEngine; using System.Collections;

 public class ControlloNave : MonoBehaviour {
 
     public float RotV;
     public float TravelV;
 
     // Use this for initialization
     void Start () {
         Debug.Log("The movement script is loaded");
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 
     void FixedUpdate()
     {
 
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
         GetComponent<Rigidbody2D>().transform.Rotate(0, 0, moveHorizontal*RotV);
         GetComponent<Rigidbody2D>().AddForce(moveVertical* transform.right * TravelV,ForceMode2D.Force);
 
     }
 }

If possible, I also aim to forbid the user from getting negative acceleration values (I don't want the ship to go backward).

Thanks for the help!

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

1 Reply

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

Answer by MD_Reptile · May 01, 2016 at 08:25 AM

GetComponent < Rigidbody2D >().AddRelativeForce(....);

That should do it, instead of using AddForce. Probably should keep a reference to that rigidbody2d instead of using GetComponent every time:

 ...
 public float TravelV;
 private Rigidbody2D thisRigidbody;
  
      // Use this for initialization
      void Start () {
          Debug.Log("The movement script is loaded");
          thisRigidbody = GetComponent < Rigidbody2D > ();
      }
 
 ...


Comment
Add comment · Show 4 · 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 GiacomInox · May 01, 2016 at 08:42 AM 0
Share

I replaced the argument, but now the ship moves in see$$anonymous$$gly random directions. How does AddRelativeForce work?

avatar image MD_Reptile GiacomInox · May 01, 2016 at 09:20 AM 0
Share

Try AddRelativeForce (Vector2.up)

avatar image GiacomInox MD_Reptile · May 01, 2016 at 09:24 AM 0
Share

That worked. Thank you very much.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

My character moves in seemingly random directions. 1 Answer

Hey everyone. I need some help please. Does any of you know a Bike controller/physics tutorial? ,does anyone know any tutorials on bike movement and Physics? 2 Answers

Physics-based controls and slopes ? 2 Answers

How do I nullify the torque from colliding but not from player input? (2D,C#) 1 Answer

2D 360 degress platformer example needed 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