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 /
  • Help Room /
avatar image
1
Question by martinboisrond · Apr 10, 2018 at 08:50 AM · rotationanglerotation axistrigonometryrotation detection

Rotating in relation to touch X on screen

Hello everyone !

It's been 3 days I'm stuck on this problem which should in theory be simple. I've tried many different options but without success. I figure a more experienced programmer here might have the elegant solution to this.

Basically I have my camera nested as a child, with a 0f, -0.9f, 0f local position to a parent "rotater" object at the center of the world. That object rotates on X. While to player applies a touch on screen, I'm calculating something like:

 float perc = Input.mousePosition.x/Screen.width;
 
 float rotaterXRot = 360f * perc;
 
 rotater.transform.rotation = Quaternion.Euler(new Vector3(rotaterXRot, 0f,0f));


(I do substract the delta of the touch and the actual equivalent on screen X in % of the rotater's rotation, so when I press the screen the object doesnt "teleport", and left or right motion will only rotate from the rotation the object is at at the time of the touch.)

It works fine left and right until 90 degrees if I move to the right and 270 if I move to the left. Once I overshoot that 180f around the start position, the rotation jumps to weird values.

My question is, is there any simple way for me to translate my touch.x in % of the screenwidth to rotate my object along a 360f angle where 180f is where the x rotation is at the time of the touch (If when I press my object is currently rotated to -174 degrees on x, this should mark the 180f in my 360f) ? (here is a graphic to explain more explicitly the situation: alt text

forumhelpgraphic.png (34.3 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

Answer by martinboisrond · Apr 11, 2018 at 12:05 PM

Edit: I do take the current localEulerAngles as a vector3 at the time of the press, then add it to the % of 360f rotation according the the touch X pos, to finally enter these two as the new target Rotation for the object:

 lastRot = rotater.transform.localEulerAngles;
 // then later
 float rotX = percOfTouchX * 360f;
 rotX += 180f;
 Vector3 tempV3 = new Vector3 (adjustedRot, 0f, 0f);
 rotater.transform.rotation = Quaternion.Euler (tempV3+lastRot);
 

This works but again the problem is when I touch 90 degrees, the angle goes down to zero instead of continuing past 90 ---> ...88, 89, 90, 88, 89... Same thing for 270. I believe this what is causing my problem. It's also the value that printing rotater.transform.eulerAngle.x gives me. Any idea how I could avoid this please ?

Comment
Add comment · 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

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

162 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

Related Questions

Problems with limiting angles 1 Answer

How can i get Transform component rotation value which seen in editor inspector? 0 Answers

How to find a position's angle around an arbitrary axis? 1 Answer

How to rotate a cube vertically when the axis is unknown 0 Answers

How to make a gameObject's Y rotation being another gameObjects' X rotation 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