• 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 alexanderflink · Apr 02, 2013 at 07:17 PM · camerarotationtransformvector3distance

Keep camera at certain distance from character when rotating

Hello. I'm trying to create a third person controller by myself for training purposes. Everything has gone well so far, but i'm stuck at making the camera follow the player at a certain distance (5 units), no matter it's orientation around the player. I know there are a couple of other threads, which I have read. But either they are not explanatory enough, or don't solve my problem.

I know I can get the distance from the camera to the character by subtracting the camera's position from the player's position and getting the magnitude from that Vector3. However, I do not know how to apply this distance to the camera's position, since I assume that you can't add a float to a Vector3.

A detailed explanation of how to solve this would be greatly appreciated.

Thanks Alex

Comment
Add comment · Show 4
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 LyanApps · Apr 02, 2013 at 07:40 PM 0
Share

I think you want to multiply ins$$anonymous$$d of add a float to a Vector3. scalar Vector3 is equivalent to new Vector(vector3.x scalar, vector3.y scalar, vector3.z scalar);

avatar image alexanderflink · Apr 02, 2013 at 08:02 PM 0
Share

I'm sorry, I didn't understand that at all. What is a scalar?

avatar image LyanApps · Apr 02, 2013 at 08:19 PM 0
Share

It's just a magnitude or an integer. Vector3(0,0,10) = 10 * Vector(0,0,1)

avatar image alexanderflink · Apr 02, 2013 at 08:21 PM 0
Share

As stated below, the camera's position and angle is being manipulated through transform.RotateAround(player). It is not supposed to be fixed to a point behind the player, but I still want it to keep a certain distance from the player when it moves.

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by LyanApps · Apr 02, 2013 at 07:38 PM

I would say try thinking your method in reverse. Instead of trying to find the distance from the camera to the player object and then applying that. You already know what distance you want, and I assume you know what position around the player you want your camera to be positioned at. So you should think about positioning the camera based on the player's location.

 //Distance away from player you want to be at.
 var distance = 10.0f;
 //Direction from the player you want to  be at.
 //Use a unit vector which can be found from any Vector3 using .Normalize
 //This vector is pointing behind and above the player at a 45deg angle
 var direction = new Vector3(-10, 10, 0);
 //distance * direction. normalized(); scales the unit vector to a constant size of distance behind the player.
 camera.position = player.position + distance * direction. normalized();
Comment
Add comment · Show 2 · 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 alexanderflink · Apr 02, 2013 at 07:49 PM 0
Share

Oh, I'm sorry. I forgot to mention that the camera is being rotated around the player using transform.RotateAround, when the right stick is pushed right, left, up or down. This means that the direction from the player is not always the same...

avatar image LyanApps · Apr 02, 2013 at 08:34 PM 0
Share

So can you have an empty game object that's position is equal to the player's position. Rotate that using your transform.RotateAround. Then in the given code change: var direction = go.forward;

If you think about it, what you're doing is putting your camera on a spherical plane around your player object. Using an empty GameObject would be equivalent of defining the center point of your sphere. The last line in the given code would essentially cast a ray out from the center of the sphere in direction and distance to map to a point where your camera should be positioned.

avatar image
1

Answer by sparkzbarca · Apr 02, 2013 at 08:31 PM

get the slope of the line of the from the player to the camera normalize

multiply by 5 assign that to the camera

that'd be done as

 camera.transform.position = (camera.transform.position - player.transform.position).normalized * 5;



or

get the slope of the line from player to player to the camera use that as a ray so you have origin and direction

get the point 5 units along the ray ray.point(5)

assign that to the camera

mark as answered 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
avatar image
0

Answer by mhtraylor · Apr 02, 2013 at 08:49 PM

You could use the very useful Mathf.Clamp method on the camera's transform. For instance, when moving the camera you would "clamp" its position to always be at your specified distance (e.g. 5 units in your example). Mathf.Clamp in the Scripting Reference

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

13 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

Related Questions

nullifying targetObject after a Smooth LookAt Transition 2 Answers

Player rotation = Camera Rotation 0 Answers

How do i lock the position of the camera above the player relative to the origin point? 0 Answers

Camera distance from mesh not center 1 Answer

Rotating object on one axis using Input.GetAxis 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