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
1
Question by Simon V · Jul 02, 2011 at 02:20 PM · cameradistanceangle

Calculate vector3 from angle

I'm currently making a camera, but I'm having trouble finding out a ventor3 from an angle.

I want the camera to be able to rotate around my character. I'm using mouseX and mouseY axes as input. Now, if I have an angle, how do I calculate the vector3 if it has to be x units away from the character.

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

2 Replies

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

Answer by Peter G · Jul 02, 2011 at 03:39 PM

You can do the trig if you want to (its really easy), but Unity has a bult in operator to do the math for you.

 var distFromObj : float;
 
 function SetVectorFromAngle (x : float, y : float, z : float) : Vector3 {
      var rotation = Quaternion.Euler(x, y, z);
      var forward = Vector3.forward * distFromObj;
      return forward * rotation;
 }

multiplying a quaternion by a vector rotates the vector by the quaternion. So for example:

 var rotation = Quaternion.AngleAxis(90, Vector3.up);
 var forward = Vector3.forward;
 var right = forward * rotation;
Comment
Add comment · Show 4 · 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 02, 2011 at 03:41 PM 0
Share

Didn't know the multiplication was possible :) Thanks!

avatar image FenixShadow · Jan 30, 2018 at 11:57 PM 3
Share

A slight correction to your code: the Quaternion should come first in the multiplication. It throws an error if you try to put the Vector3 first. That little difference got me for a bit.

avatar image giboulot FenixShadow · Jun 09, 2020 at 07:24 PM 0
Share

Thanks, i was wondering why i couldn't multiply a vector3 and a quaternion :)

avatar image JVLVince · Apr 10, 2019 at 04:00 AM 0
Share

Hi sir, thanks for your reply to this question, it's helped me a lot. This must be a noob question but could you please explain for me why multiply a vector with a quaternion can get the direction of the other vector? I try to explain it myself but I have no idea. :( Thanks and regard Vince

avatar image
2

Answer by Bunny83 · Jul 02, 2011 at 03:55 PM

Well, the most straight forward way that came to my mind is simple trigonometry.

 //C#
 float radHeading = heading*Mathf.Deg2Rad;
 float radPitch = pitch*Mathf.Deg2Rad;
 Vector3 relDirection = new Vector3(Mathf.Cos(radHeading),Mathf.Sin(radHeading),0);
 relDirection *= Mathf.Cos(radPitch);
 relDirection.z = Mathf.Sin(radPitch);

 // The camera position would be your target postion (the player's position) + relDirection*dist;
 // You might need to invert one of the angles to match your setup (invert an angle will reverse the direction).
 transform.position = target.position + relPosition * distance;

Another, more Unity like, way would be to place your camera into an empty GameObject and move the camera locally to (0,0,-distance). Now you can place the empty GO at the player position and just need to rotate it.

 transform.rotation = Quaternion.AngleAxis(pitch,Vector3.right) * Quaternion.AngleAxis(heading,Vector3.up)

Something like that ;)

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I get an Unity Camera to interact with an Android camera compas funtion 1 Answer

How to calculate direction between 2 objects 2 Answers

How to make camera position relative to a specific target. 1 Answer

Camera Zoom from rotation. 0 Answers

Why is my terrain dark in the distance? (It gets lighter when moving closer.) 4 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