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;
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
add a comment |
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
add a comment |
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
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
c# unity3d
edited Mar 24 at 19:05
double-beep
3,12451632
3,12451632
asked Mar 24 at 18:57
RemousamaviRemousamavi
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 27 at 21:35
FacePuncher7FacePuncher7
866
866
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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