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 CiberX15 · Feb 06, 2014 at 07:50 PM · velocitynavigationspacegame

Add Drag on one local axis to prevent fish tail of 2D Spaceship

That is one long question huh? So here is what I am trying to do. I am using an object with a Rigidbody2D for a top down 2D space combat RPG. When the player gives a ship a move order, I want the ship to rotate slowly around and begin thrusting at its destination when its current angle and desired angle reach less than 30 degrees. This much I have working.

However, since it begins thrusting before it is perfectly aimed, picks up some sideways velocity, causing it to keep rotating to face its target because of the drift. Thus the ship fishtails around its target, which looks really silly.

So what I want to do is apply drag on the local x velocity so that the ship can compensate for the incorrect velocity (similar to what RCS thrusters do for real space ships).

Here is the relevant code I have now:

 function Navigate()
 {
     if(Destination != null)
     {
         var newRotation = Quaternion.LookRotation(transform.position - Destination.position, Vector3.forward);
 
         newRotation.x = 0.0;
 
         newRotation.y = 0.0;
 
         transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * RotSpeed);
         
        
         var TargetAngle = Vector3.Angle(-transform.up, Vector3.Normalize(transform.position - Destination.position));
         
         Debug.Log("Angle to target = " + TargetAngle);
          
         if(TargetAngle < 30)
         {
             Debug.Log("Begin move");
             var correction : float = TargetAngle / 30;
             rigidbody2D.AddForce(transform.up * ShipSpeed * correction);
             
             rigidbody2D.velocity = Vector2.Scale(rigidbody2D.velocity, (Vector2(transform.up.x, transform.up.y) * 0.5));
         }
     }
 }

Thanks in advance for any help you can provide : D

Comment
Add comment · Show 1
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 CiberX15 · Feb 07, 2014 at 04:46 AM 0
Share

Still having trouble with this : P

1 Reply

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

Answer by robertbu · Feb 07, 2014 at 05:09 AM

I have no idea if this is the right solution for your problem. It seems to me you'd want to just increase the drag in general and then up the force to compensate a bit. But if you want to add a drag-like change to the velocity, you may do it this way:

 rigidbody2D.velocity.x = rigidbody2D.velocity.x * dragFactor;

'dragFactor' will be some number close to but less than 1.0 like 0.985. This needs to be executed in FixedUpdate().

Comment
Add comment · Show 3 · 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 CiberX15 · Feb 07, 2014 at 04:54 PM 0
Share

$$anonymous$$ind of right, but that woulden't that only effect the global x velocity. The ship rotates so velocity.x wont always be its local x axis.

avatar image robertbu · Feb 07, 2014 at 07:08 PM 1
Share

Untested: use Transform.InverseTransformDirection() to transform velocity to local coordinates, scale, then transform it back:

 var dir = transform.InverseTransformDirection(rigidbody.velocity);
 dir.x = dir.x * dragFactor;
 rigidbody.velocity = transform.TransformDirection(dir);
avatar image CiberX15 · Feb 07, 2014 at 07:32 PM 0
Share

Oh interesting, I shall try that.

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

18 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

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

GameObject won't move when I attach an Animation Controller 2 Answers

Rigidbody Disable Velocity/Movement? 1 Answer

force.normalized * magnitude 1 Answer

Rigidbody velocity capped at 0.4 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