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
0
Question by Copycat97 · Apr 01, 2019 at 05:46 AM · rotation2d gamerigidbody2dhingejoint2dstrange

Strange Problem with Torque-based Rotation

I am trying to get the arm of a character to face the mouse at all times.

The way I am doing this is using torque. The script will add more force the bigger the angle difference between the arm and the mouse is.

However, the arm doesn't seem to face the correct direction. The arm looks left when the mouse is left and right when the mouse is right, but it looks up when the mouse is below and viseversa, which is not suppose to happen.

Oddly enough when I try to use the angle difference * -1, the arm looks right when the mouse is left (and viseversa), but it looks up when the mouse is up.

In other words, using the normal angle difference, only the vertical direction is flipped, but when the angle difference is made opposite, only the horizontal direction is flipped.

I have never encountered such strange problem, and I have no idea what in my script is causing it. The game is a 2D game and this arm is attached to the player body using HingeJoing2D.

Here's my script as of now:

public class ArmController : MonoBehaviour { private Rigidbody2D rb; private HingeJoint2D pivot; private Vector2 curPos; private Vector2 pivotPos; private Vector2 direction; private float pivotAng; private float angleDif; // Use this for initialization void Start() { rb = GetComponent<Rigidbody2D>(); pivot = GetComponent<HingeJoint2D>(); Cursor.visible = true; UpdatePivot(); } private void FixedUpdate() { } // Update is called once per frame void Update() { UpdatePivot(); //update angle difference angleDif = VectorToAngle(direction) - pivot.jointAngle; //this is here to keep the angle between -180 and 180 angleDif = angleDif % 360; if (angleDif > 180) { angleDif = angleDif - 360; } else if (angleDif < -180) { angleDif = angleDif + 360; } TorqueAtMouse(); Debug.Log("cursor: "+ VectorToAngle(direction)); Debug.Log("pivot: " + pivotAng); Debug.Log("delta: " + angleDif); Debug.Log("angular velocity: "+ rb.angularVelocity); } /* * (delta >= 2 && delta <= 180) || (delta >= 362 && delta <= 540) || (delta >= 358 && delta <= -180) * (delta <= -2 && delta >= -180) || (delta <= 358 && delta >= 180) || (delta <= -362 && delta >= -540) */ protected void UpdatePivot() { pivotPos = pivot.transform.position; curPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); direction = curPos - pivotPos; /* pivotAng = pivot.jointAngle; if (pivotAng > 360 || pivotAng < -360) { pivotAng = pivotAng % 360; if(pivotAng > 180) { pivotAng = pivotAng - 360; }else if(pivotAng < -180) { pivotAng = pivotAng + 360; } //pivotAng - 360 * Mathf.Floor(pivotAng+180 / 360) }*/ } void TorqueAtMouse() { if ((angleDif >= 3 && angleDif <= 180) || (angleDif >= 363 && angleDif <= 540) || (angleDif >= 357 && angleDif <= -180)) { rb.AddTorque(-angleDif / 30); Debug.Log("Turning +"); } else if ((angleDif <= -3 && angleDif >= -180) || (angleDif <= 357 && angleDif >= 180) || (angleDif <= -363 && angleDif >= -540)) { rb.AddTorque(-angleDif / 30); Debug.Log("Turning -"); } else { rb.AddTorque(-rb.angularVelocity * rb.mass); } if (rb.angularVelocity > 1000 || rb.angularVelocity < -1000) { rb.angularVelocity = 0; } } protected float VectorToAngle(Vector2 vector) { return Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg; } }

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

0 Replies

· Add your reply
  • Sort: 

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

156 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

Related Questions

Rigidbody2D keeps rotating when freeze position is checked 1 Answer

Rigidbody rotation: Use hinge joint motor or MoveRotation script? 0 Answers

Hingejoint2D makes a spin before getting into the angle limits. 1 Answer

,Rotating a 2D sprite with Input 0 Answers

How do I instantly change the direction of rotation of a gameobject? 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