Trying to make a Rigidbody2D face a target using AddTorqueHow to make HTTP POST web requestTry-catch speeding up my code?Determine if clockwise or counter-clockwise rotation is faster in Unity C#Adding an integer to the rotation of a camera past 360 degreesWhen rotating 2D sprite towards cursor via angularVelocity, it spins at one pointCalculating new angle for player to look at some 3d world objectUnity head Y axis rotation clampUnity 3D - Move Humanoid head muscles using scriptRotating an object dependant on z-axis attitude of gyroscope sensorChange animation based on mouse screen position in Unity 2D

How to ensure color fidelity of the same file on two computers?

How can I get an unreasonable manager to approve time off?

Who enforces MPAA rating adherence?

Why are trash cans referred to as "zafacón" in Puerto Rico?

With Ubuntu 18.04, how can I have a hot corner that locks the computer?

Electricity free spaceship

Why not invest in precious metals?

Teaching a class likely meant to inflate the GPA of student athletes

Finding value of expression with roots of a given polynomial.

Second (easy access) account in case my bank screws up

Why am I getting a strange double quote (“) in Open Office instead of the ordinary one (")?

Non-aqueous eyes?

Determining fair price for profitable mobile app business

Russian word for a male zebra

Why was this person allowed to become Grand Maester?

Minimum distance between two connectors in a high voltage PCB design

Why we don’t make use of the t-distribution for constructing a confidence interval for a proportion?

Is using 'echo' to display attacker-controlled data on the terminal dangerous?

How to trick the reader into thinking they're following a redshirt instead of the protagonist?

Thread Pool C++ Implementation

Check if three arrays contains the same element

Writing an augmented sixth chord on the flattened supertonic

Has there been a multiethnic Star Trek character?

Does Disney no longer produce hand-drawn cartoon films?



Trying to make a Rigidbody2D face a target using AddTorque


How to make HTTP POST web requestTry-catch speeding up my code?Determine if clockwise or counter-clockwise rotation is faster in Unity C#Adding an integer to the rotation of a camera past 360 degreesWhen rotating 2D sprite towards cursor via angularVelocity, it spins at one pointCalculating new angle for player to look at some 3d world objectUnity head Y axis rotation clampUnity 3D - Move Humanoid head muscles using scriptRotating an object dependant on z-axis attitude of gyroscope sensorChange animation based on mouse screen position in Unity 2D






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Suppose I have a player-ship and a planet in a 2D unity game. I want to make the ship's rotation fall level relative to the surface directly under it (For example if the part of the surface under the ship is 80 degrees, the ship should tend to fall to 80 degrees while it's over that spot).



I currently have a code that kind-of works:



 Vector2 direction = planet.position - transform.GetChild(0).position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + 90;
rig[0].AddTorque(-rig[0].rotation + angle);


It does what I want except for the spot where the ship's rotation transitions from 360 to 0 where it does a sudden 360 flip. I'm trying to figure out how to make that not happen.



I don't want to use LookAt() for this because this is supposed to be a torque that tends towards the target direction, not a snap-set to that direction.










share|improve this question






























    0















    Suppose I have a player-ship and a planet in a 2D unity game. I want to make the ship's rotation fall level relative to the surface directly under it (For example if the part of the surface under the ship is 80 degrees, the ship should tend to fall to 80 degrees while it's over that spot).



    I currently have a code that kind-of works:



     Vector2 direction = planet.position - transform.GetChild(0).position;
    float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + 90;
    rig[0].AddTorque(-rig[0].rotation + angle);


    It does what I want except for the spot where the ship's rotation transitions from 360 to 0 where it does a sudden 360 flip. I'm trying to figure out how to make that not happen.



    I don't want to use LookAt() for this because this is supposed to be a torque that tends towards the target direction, not a snap-set to that direction.










    share|improve this question


























      0












      0








      0








      Suppose I have a player-ship and a planet in a 2D unity game. I want to make the ship's rotation fall level relative to the surface directly under it (For example if the part of the surface under the ship is 80 degrees, the ship should tend to fall to 80 degrees while it's over that spot).



      I currently have a code that kind-of works:



       Vector2 direction = planet.position - transform.GetChild(0).position;
      float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + 90;
      rig[0].AddTorque(-rig[0].rotation + angle);


      It does what I want except for the spot where the ship's rotation transitions from 360 to 0 where it does a sudden 360 flip. I'm trying to figure out how to make that not happen.



      I don't want to use LookAt() for this because this is supposed to be a torque that tends towards the target direction, not a snap-set to that direction.










      share|improve this question
















      Suppose I have a player-ship and a planet in a 2D unity game. I want to make the ship's rotation fall level relative to the surface directly under it (For example if the part of the surface under the ship is 80 degrees, the ship should tend to fall to 80 degrees while it's over that spot).



      I currently have a code that kind-of works:



       Vector2 direction = planet.position - transform.GetChild(0).position;
      float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + 90;
      rig[0].AddTorque(-rig[0].rotation + angle);


      It does what I want except for the spot where the ship's rotation transitions from 360 to 0 where it does a sudden 360 flip. I'm trying to figure out how to make that not happen.



      I don't want to use LookAt() for this because this is supposed to be a torque that tends towards the target direction, not a snap-set to that direction.







      c# unity3d






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 24 at 19:05









      double-beep

      3,12451632




      3,12451632










      asked Mar 24 at 18:57









      RemousamaviRemousamavi

      1




      1






















          1 Answer
          1






          active

          oldest

          votes


















          0














          A rough work-around I've used in the past is:



          Create 2 objects and make them children of your player object.



          Position one of them at (-1,0,0) locally and call it LeftObj. Position the other at (1,0,0) and call it RightObj.



          Now you can simply check which is closer to target to figure out which is the most efficient way to turn.



          float LDist = Vector3.Distance(LeftObj.transform.position, target.transform.position);

          float RDist = Vector3.Distance(RightObj.transform.position, target.transform.position);

          if (LDist < RDist)

          // Turn left

          else

          // Turn right




          Not the most efficient method, but it's simple and quick to set up, and tells you clearly "left or right" regardless of rotation values.



          The obvious drawback is that you can't get a rotation value to scale your torque with as you get closer to a target, so you may need to increase the 'angularDrag' on your player Rigidbody to smooth out some stuttering.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327379%2ftrying-to-make-a-rigidbody2d-face-a-target-using-addtorque%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            A rough work-around I've used in the past is:



            Create 2 objects and make them children of your player object.



            Position one of them at (-1,0,0) locally and call it LeftObj. Position the other at (1,0,0) and call it RightObj.



            Now you can simply check which is closer to target to figure out which is the most efficient way to turn.



            float LDist = Vector3.Distance(LeftObj.transform.position, target.transform.position);

            float RDist = Vector3.Distance(RightObj.transform.position, target.transform.position);

            if (LDist < RDist)

            // Turn left

            else

            // Turn right




            Not the most efficient method, but it's simple and quick to set up, and tells you clearly "left or right" regardless of rotation values.



            The obvious drawback is that you can't get a rotation value to scale your torque with as you get closer to a target, so you may need to increase the 'angularDrag' on your player Rigidbody to smooth out some stuttering.






            share|improve this answer



























              0














              A rough work-around I've used in the past is:



              Create 2 objects and make them children of your player object.



              Position one of them at (-1,0,0) locally and call it LeftObj. Position the other at (1,0,0) and call it RightObj.



              Now you can simply check which is closer to target to figure out which is the most efficient way to turn.



              float LDist = Vector3.Distance(LeftObj.transform.position, target.transform.position);

              float RDist = Vector3.Distance(RightObj.transform.position, target.transform.position);

              if (LDist < RDist)

              // Turn left

              else

              // Turn right




              Not the most efficient method, but it's simple and quick to set up, and tells you clearly "left or right" regardless of rotation values.



              The obvious drawback is that you can't get a rotation value to scale your torque with as you get closer to a target, so you may need to increase the 'angularDrag' on your player Rigidbody to smooth out some stuttering.






              share|improve this answer

























                0












                0








                0







                A rough work-around I've used in the past is:



                Create 2 objects and make them children of your player object.



                Position one of them at (-1,0,0) locally and call it LeftObj. Position the other at (1,0,0) and call it RightObj.



                Now you can simply check which is closer to target to figure out which is the most efficient way to turn.



                float LDist = Vector3.Distance(LeftObj.transform.position, target.transform.position);

                float RDist = Vector3.Distance(RightObj.transform.position, target.transform.position);

                if (LDist < RDist)

                // Turn left

                else

                // Turn right




                Not the most efficient method, but it's simple and quick to set up, and tells you clearly "left or right" regardless of rotation values.



                The obvious drawback is that you can't get a rotation value to scale your torque with as you get closer to a target, so you may need to increase the 'angularDrag' on your player Rigidbody to smooth out some stuttering.






                share|improve this answer













                A rough work-around I've used in the past is:



                Create 2 objects and make them children of your player object.



                Position one of them at (-1,0,0) locally and call it LeftObj. Position the other at (1,0,0) and call it RightObj.



                Now you can simply check which is closer to target to figure out which is the most efficient way to turn.



                float LDist = Vector3.Distance(LeftObj.transform.position, target.transform.position);

                float RDist = Vector3.Distance(RightObj.transform.position, target.transform.position);

                if (LDist < RDist)

                // Turn left

                else

                // Turn right




                Not the most efficient method, but it's simple and quick to set up, and tells you clearly "left or right" regardless of rotation values.



                The obvious drawback is that you can't get a rotation value to scale your torque with as you get closer to a target, so you may need to increase the 'angularDrag' on your player Rigidbody to smooth out some stuttering.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 27 at 21:35









                FacePuncher7FacePuncher7

                866




                866





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327379%2ftrying-to-make-a-rigidbody2d-face-a-target-using-addtorque%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현