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
0
Question by samsmithnz · Oct 10, 2013 at 09:30 PM · rotationperpendicular

Make my camera perpendicular to two objects

I'm trying to move my camera (A) in the middle and perpendicular to two objects (at C and D). I can find the midpoint in (B), but I can't get my mind around the math that is needed to rotate my camera to be facing the mid point (B) too.

alt text

Moving it to the middle is no problem, I can use this:

     public static Vector3 GetMidPointBetweenTwoObjects(Vector3 sourceObject, Vector3 targetObject)
     {
         Vector3 result = new Vector3(0, 0, 0);
         result.x = sourceObject.x + ((targetObject.x - sourceObject.x) / 2);
         result.y = sourceObject.y + ((targetObject.y - sourceObject.y) / 2);
         result.z = sourceObject.z + ((targetObject.z - sourceObject.z) / 2);
         return result;
     }

But how can I find the rotation for my camera at A, that is perpendicular to these two objects, for point B?

Comment
Add comment · Show 1
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 robertbu · Oct 11, 2013 at 04:55 AM 0
Share

One problem with this problem as stated is that there are an infinite number of 'A' positions. That is, given an arbitrary vectors in 3D space, 'A' could wrap around the CD vector 360 degrees for any number of solutions. So you need some way of defining the plane where ABCD live. It may be this is really a 2D problem and therefore you know the plane, or you might define the plane by the camera and points C and D. Once you know the plane, take the normal of the plane and use Vector3.Cross() with the Vector BC or BD. The result will be the 90 degree vector you seek.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Tomer-Barkan · Oct 10, 2013 at 10:44 PM

To calculate the location in the middle, you can use Vector mathematics instead of doing it manually:

 Vector3 result = sourceObject + (targetObject - sourceObject) / 2;

If you want to move it along the line towards A, simply rotate the vector between D and C left by 90 degrees, then add it to B.

 Vector3 midCD = sourceObject + (targetObject - sourceObject) / 2;
 Quaternion ninetyLeft = Quaternion.AngleAxis(90, Vector3.up); // to rotate 90 degrees left, rotate 90 degrees around the Y axis
 Vector3 towardsA = (ninetyLeft * (targetObject - sourceObject)).normalized; // rotate the vector that points from C to D by 90 degrees left, then you get the vector that points from B to A. Normalize to make it of size 1.
 Vector3 result = midCD + towardsA * distanceTowardsA;

Then to put the camera and make it face point B, you can use transform.position and transform.LookAt()`:

 transform.position = result;
 transform.LookAt(midCD);

 
Comment
Add comment · Show 6 · 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 samsmithnz · Oct 11, 2013 at 03:52 AM 0
Share

it's close, but there is something funky about the ninetyLeft rotation, it doesn't seem to appear 90 degrees all the time.

avatar image Tomer-Barkan · Oct 11, 2013 at 06:45 AM 0
Share

That doesn't make senses, must be something else... can you share your code and maybe screenshots of whats happening?

avatar image Tomer-Barkan · Oct 11, 2013 at 06:49 AM 0
Share

Oh, and I assumed that Y should always be 0 (ie I made the problem 2d). If it's not, then the answer might differ a bit, but then you have to explain how you want it to behave, because you didn't mention a 3rd dimension at all in your problem.

avatar image samsmithnz · Oct 11, 2013 at 11:24 AM 0
Share

I guess that is it. It is 3d space. Here is my sandbox level.

You can see my two characters. The red dot is the mid point. The green dot, way in the distance in the spotlight is supposed to be A.

alt text

locations:

C: (0, 0.5, 10)

D: (0, 0.5, 5)

B: (0, 0.6, 7.5) [B Note: Interesting that B adds that 0.1 to Y...]

A: (-1, 0, 0) [A Note: I would have expected something at about (-1, 0.5, 7.5)

And here is my code:

     public IEnumerator $$anonymous$$oveCameraInIsometricViewOverBetweenTwoObjects(Vector3 sourceLocation, Vector3 targetLocation)
     {
         //Get the midpoint 
         Vector3 midCD = sourceLocation + (targetLocation - sourceLocation) / 2;
 
         //Create a red cube at the midCD point
         CreateGameObjectAtLocation(midCD, Color.red);
 
         // to rotate 90 degrees left, rotate 90 degrees around the Y axis
         Quaternion ninetyLeft = Quaternion.AngleAxis(90, Vector3.up);
         // rotate the vector that points from C to D by 90 degrees left, then you get the vector that points from B to A. Normalize to make it of size 1.
         Vector3 towardsA = (ninetyLeft * (targetLocation - sourceLocation)).normalized;
 
         //Create a green cube at the towardsA point
         CreateGameObjectAtLocation(towardsA, Color.green);
     }


avatar image Tomer-Barkan · Oct 11, 2013 at 12:04 PM 0
Share

lol, you used towardsA as the position for the new object. towardsA is the vector that points from B to A. So to get the position, you need to add it to B.

Notice that in my example, the result is not towardsA, but midCD + towardsA

To fix, change line 15 to:

 CreateGameObjectAtLocation(midCD + towardsA, Color.green);

And you should get exactly what you expect, (-1, 0.5, 7.5).

Show more comments
avatar image
0

Answer by DaveA · Oct 10, 2013 at 10:22 PM

If you have B, and your camera is at A, then use transform.LookAt(B); in a script on the camera object.

Comment
Add comment · Show 1 · 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 samsmithnz · Oct 11, 2013 at 03:52 AM 0
Share

Unfortunately this isn't quite what I'm looking for, as my camera isn't at A. If it was, you're right, it would be easy to just look.

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

17 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

Related Questions

Rotate camera 90 degrees from target, and follow ... demo not working properly 1 Answer

Solved | Rotating Object in Single Axis while staying Perpendicular to a Rotating Plane 1 Answer

Flip over an object (smooth transition) 3 Answers

Rotate object to lookAt point and be perpendicular to the ground 1 Answer

Changing the forward rotation for LookAt 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