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 Simon V · Jul 02, 2011 at 09:03 PM · vector3distanceangle

Calculate position of camera with set distance and angle.

Despite having my last question answered >>CLICK<<, I still can't wrap my head around it. I might have to rephrase my question:

I can adjust the angle between my camera and my character, by using the MouseX and MouseY axes.

When I have this angle, I want my camera to be x units away from the character, no matter the angle. How can I calculate a vector3 with this input?

Could you please also tell me how I represent this distance between the camera and the character (int, vector3,..)?

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 SilverTabby · Jul 02, 2011 at 09:26 PM 0
Share

Let me get this straight:

You have a character, and you have a camera that moves in a sphere around the character based on mouse input.

So you want the camera to maintain a set distance from the character to the camera no matter where the camera is or whatever angle the camera is at? Is that what you are asking? Did I miss or misread something?

2 Replies

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

Answer by aldonaletto · Jul 02, 2011 at 11:11 PM

If you subtract the camera position from the player's position, the result is a vector indicating the direction camera->player, and its magnitude will be the distance between their pivots (positions). Supposing cam and player are variables containing the camera and player transforms:

   var vCam:Vector3 = player.position - cam.position; // vector camera->player
   var dist:float = vCam.magnitude; // distance between camera and player

Or, if you want to get a position at dist distance from the player, and rotated V degrees in the vertical plane and H degrees in the horizontal plane, you can do:

   var vCam:Vector3 = Quaternion.Euler(V,H,0) * -Vector3.forward;
   cam.position = player.position - vCam * distance;

This rotates Vector3.forward V and H degrees as specified above, and place the camera at the distance dist in that direction.
NOTE: depending on the reference you have, you may find the V angle going in the opposite direction; if it happens, use -V instead. The same applies to the H angle - if you expect it to go clockwise, but it goes counter-clockwise, use -H.

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 Simon V · Jul 03, 2011 at 02:32 PM 0
Share

This is exactly what I needed. Thanks!

avatar image
1

Answer by Waz · Jul 02, 2011 at 09:36 PM

If X is the longitudinal angle (0 to 360) and Y is the latitudinal (-90 to +90), you could:

 camera.transform.position = player.transform.position + Quaternion.Euler(Y,X,0);

You probably also want:

 camera.Transform.LookAt(player.transform);

Also note that X should keep going past 0 and 360 when the user move the mouse further.

There is already a camera script in Standard Assets for this (MouseOrbit).

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 Simon V · Jul 03, 2011 at 02:31 PM 0
Share

Thank you very much!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Interact with objects based on distance. 1 Answer

Find Vector3 from a starting Vector3, angle, and distance. 1 Answer

Vector3.Distance edge of model 3 Answers

Distance Variable Won't Change. What's Wrong With My Script? 1 Answer

How to calculate direction between 2 objects 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