Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 mh1060 · Aug 10, 2021 at 02:24 PM · rotationtranslate

Rotating gameobject while maintaining velocity

I'm working on getting a little spaceship flying around but can't get the movement just how I want it.

My requirements:

  • Pressing 'up' accelerates the ship (caps at max speed)

  • Ship maintains speed (only way to slow down is turning around and accelerating the other direction)

  • Pressing 'left' or 'right' rotates ship without changing velocity

After doing plenty of googling and trial & error, I came across this code which checked most of the boxes except for one.

 public class TransformFunctions : MonoBehaviour
  {
      public float turnSpeed = 50f;
  
      public float _Velocity = 0.0f;      // Current Travelling Velocity
      public float _MaxVelocity = 1.0f;   // Maxima Velocity
      public float _Acc = 0.0f;           // Current Acceleration
      public float _AccSpeed = 0.1f;      // Amount to increase Acceleration with.
      public float _MaxAcc = 1.0f;        // Max Acceleration
      public float _MinAcc = -1.0f;       // Min Acceleration
  
  
      void Update()
      {
  
          if (Input.GetKey(KeyCode.UpArrow))
              _Acc += _AccSpeed;
  
          if (Input.GetKey(KeyCode.DownArrow))
              _Acc -= _AccSpeed;
  
          if (Input.GetKey(KeyCode.LeftArrow))
              transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
  
          if (Input.GetKey(KeyCode.RightArrow))
              transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
  
  
          if (_Acc > _MaxAcc)
              _Acc = _MaxAcc;
          else if (_Acc < _MinAcc)
              _Acc = _MinAcc;
  
          _Velocity += _Acc;
  
          if (_Velocity > _MaxVelocity)
              _Velocity = _MaxVelocity;
          else if (_Velocity < -_MaxVelocity)
              _Velocity = -_MaxVelocity;
  
          transform.Translate(Vector3.forward * _Velocity * Time.deltaTime);
      }
  }

This code unfortunately doesn't allow for the ship to turn while maintaining it's velocity. I tried messing around with local and world space but adding Space.World to transform.Translate(Vector3.forward * _Velocity * Time.deltaTime); fixes the turning issue while of course breaking the movement as seen in the second clip below.

alt text

At this point, my only guess would be that I need to maybe stop using Vector3.forward in my transform.Translate and instead, calculate that Vector3, but I'm still pretty new to this and figured I would ask to see if anyone had any thoughts before diving right back in to google.

Thanks for reading!

examplegif.gif (516.9 kB)
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
0
Best Answer

Answer by FlaSh-G · Aug 10, 2021 at 02:54 PM

The script is moving the ship in the direction it is facing (Vector3.forward, with Transform.Translate using local space by default, so "my forward").

If you don't want that, you need to store the movement direction of the object in your script and apply that movement direction regardless of direction.

 public class SpaceshipMovement : MonoBehaviour
 {
   private Vector3 velocity;
   public float acceleration = 20f;
   public float turnSpeed = 50f;
 
   private void FixedUpdate()
   {
     if (Input.GetKey(KeyCode.UpArrow))
     {
       velocity += transform.forward * acceleration * Time.deltaTime;
     }
 
     if (Input.GetKey(KeyCode.DownArrow))
     {
       velocity -= transform.forward * acceleration * Time.deltaTime;
     }
 
     if (Input.GetKey(KeyCode.LeftArrow))
     {
       transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
     }
 
     if (Input.GetKey(KeyCode.RightArrow))
     {
       transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
     }
     
     transform.Translate(velocity * Time.deltaTime, Space.World);
   }
 }

This would be the code for sticking with you current approach, but I'd like to point out that what you want looks very much like a good use case for a Rigidbody2D component, which would take care of velocity and acceleration for you.

Comment
Add comment · Show 1 · 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 mh1060 · Aug 10, 2021 at 03:16 PM 0
Share

Thanks for the quick reply! That was exactly what I was looking for. Now time to read up on Rigidbody2D

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

165 People are following this question.

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

Related Questions

Look Where You're Going 3 Answers

Ellipse Animation by code 2 Answers

Rotate prefab to face movement directio 2 Answers

[SOLVED]How to change Quaternion by 180 degrees? 5 Answers

How could I make my camera move around a point during runtime? 1 Answer


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