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
2
Question by normalmaps · Jan 08, 2015 at 08:55 PM · rotationangleseuler

2 axis rotated towards 2 different points

Hello everyone,

I've been cracking my head over this for a while now, so I thought why not ask the creative awesome people on the unity forums?

I have a situation where the player is walking around a spherical object, the y-axis should be the vector between the position of the player and the center of the sphere. Then he should rotate around the y-axis towards a point without messing up the rotation on the spherical object.

I've written the code for both seperately, but they can't be used combined (at least not the way I've tried)

 var direction;
 var lookRotation:Quaternion;
 var improved_rotation_position =    Vector3(hit.point.x,transform.transform.position.y,hit.point.z); 
  direction = (improved_rotation_position - transform.position).normalized;       
  lookRotation = Quaternion.LookRotation(direction);


And the other code for the spherical object:

 var bodyUp:Vector3 = transform.up;
  var gravityUp:Vector3 = (transform.position-gravityTarget.transform.position).normalized;
 var targetRotation:Quaternion = Quaternion.FromToRotation(bodyUp,gravityUp)*transform.rotation;


Multiplying didn't work (as I thought). Maybe I should work with eulerAngles only? Some help would be great thanks!

picture:alt text note that the point is not on the Z- axis of the character but it should be looking towards it on that angle alone.

EDIT: Solution

Here is a easy way to get a lookat with one axis using eulerAngles:

To get the angle between 2 Vectors use

 Vector3.Angle(V1,V2);

note that it measures the angle between the 2 vectors with the middle point being Vector3(0,0,0). In order to get it to use a V3 as middle point chance the upper code in this:

Vector3.Angle(V1-V3,V2-V3);

now we have the angle. There is a smaller problem that can arise using this, but with some clever thinking you can solve it. (I don't want to spoil the ending)

result:

 transform.eulerAngles = Vector3(transform.eulerAngles.x,transform.eulerAngles.y+angle,transform.eulerAngles.z); // for rotation around the axis



objects.jpg (23.1 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
1
Best Answer

Answer by jpthek9 · Jan 08, 2015 at 09:01 PM

In a situation like this where you need to rotate on only 1 axis, use Euler angles (360 degrees).

 Vector3 MyEuler = transform.eulerAngles;
 MyEuler.y += 10 * Time.deltaTime;
 transform.eulerAngles = MyEuler;

To rotate to a specific vector on the same plane, first you have to find the angle that's needed. This can be done by subtracting the target point's X and Y coordinates from your coordinate and getting the 2 sides of a triangle. You know that it's a right angle triangle so you can just use the inverse tangent of the triangle's sides to solve for the angle needed (feel free to ask for more explanation on this if it's needed).

Use this to get the desired angle:

 TargetRotation = Direction != Vector2d.zero ? Mathd.Atan (DirNorm.y / DirNorm.x) : TargetRotation;
 

After you get the desired angle, you have to consider angle wrap around. Use a normalization method for your angles:

 void NormalizeAngle(float Angle)
 {
     int Charge = Mathf.Abs(Angle) > 180 ? -1 : 1;
     Angle = Charge * (Angle % 180);
 }
 

After you normalize your angles, simply lerp your current MyEuler.y to your desired angle or use some sort of velocity system.

Comment
Add comment · Show 8 · 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 normalmaps · Jan 08, 2015 at 09:03 PM 0
Share

and how do I make it rotate on that Euler angle towards that specific point which isn't on the line of the axis of the player? (complicated sentece, I hope i got it right)

avatar image jpthek9 · Jan 08, 2015 at 09:16 PM 1
Share

I got my math mixed up a bit. You can actually just use the inverse tangent of the y different over the x different: $$anonymous$$athf.Atan (Ydif/Xdif).

avatar image normalmaps · Jan 08, 2015 at 09:18 PM 0
Share

ok, thanks alot so far, I hope it works, gonna try it out!

avatar image normalmaps · Jan 08, 2015 at 09:49 PM 0
Share

I can't really get it done like that, so I decided to draw the situation:

alt text

how do I get that angle? using that inverse tangent gives me the point of the direction I want to be (as far as I know/might be wrong?)

edit: current direction is local

object2.jpg (34.2 kB)
avatar image jpthek9 · Jan 08, 2015 at 10:01 PM 1
Share

You don't need to get the angle between to rotate towards the target angle - just the target angle. To get the target angle, you're really looking for the angle in the picture:

alt text

In the above picture, you're solving an SAS right angle triangle (http://www.mathsisfun.com/algebra/trig-solving-sas-triangles.html). The adjacent side is the y difference and the opposite side is the x difference.

eh, this is a lot harder to explain. I posted up the code on the original answer.

untitled.png (27.3 kB)
Show more comments

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

26 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

Related Questions

How can I store the individual values of EulerAngles on a Database then re-apply 2 Answers

Change orientation 1 Answer

How do I rotate a game object in 45 degree steps per keystroke? 2 Answers

Rotation like in editor 2 Answers

How do you manually override mecanim's interpolation with rotations? 2 Answers


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