• 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 JustJunuh · Dec 27, 2016 at 04:05 AM · cameracamera followcamera movementcamera rotationthird-person-camera

[SOLVED] Third Person Spherically Orbiting Camera

Hi, everyone. So I'm on a quest to make my own third person controller, but I've hit a bit of a wall with the camera.

This is how the camera will work:

Update/Fixed Update()

//Camera position = Player Position + Camera Offset Value

//Point towards player

So that should work in theory, but the reason I'm structuring it this way is so that in order to adjust the camera to whatever position I want, I simply adjust the offset position of the camera rather than the camera itself via the right stick or another special move. The code in Update/Fixed Update will simply correct the camera's position in the next frame.

Here's the issue. How do I adjust the offset value so that it curves around the player?

Ex. http://imgur.com/a/fwZV0

I want the camera to receive a 2d direction (being up,down,left,right) and a magnitude then be able to do some math and figure out how to adjust the offset while spherically curving around the player.

Thanks for your time!

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
0
Best Answer

Answer by AlwaysSunny · Dec 27, 2016 at 04:05 AM

Don't forget to format your code with the 1010101 button.

There are many strategies for creating the type of camera behavior you're asking for.

I would recommend a strategy which uses the rotation of a dummy object to help you find the best position for the camera. Then the camera will seek this dummy object's position. All this can be done mathematically without dummy objects too, but building logical rigs of objects is often a helpful strategy.

Depending on what you want, you may need to change several things about the approach, but this should get you started:

Create your camera rig (empty game object) and use a script to make it copy the player's position each frame. (It shouldn't be a child of the player, unless you want the behavior that comes with that decision).

Use input to control the rotation of this dummy object. Give this dummy object a child and offset it from the parent some desired distance. This child object is what your camera script will seek towards, trying to occupy its position. The camera script also looks at the player.

This is the least sophisticated version of this approach, but it's a good foundation to build from.

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 JustJunuh · Dec 27, 2016 at 05:58 AM 0
Share

Thank you so much for that quick response! That solution is genius! I'm going to try and plug that in as soon as I can. I'll let you know if I stumble upon any $$anonymous$$or issues. Then I'll mark this case closed.

I do have one question though: What were you referring to when you mentioned formatting with the 1010101 button?

avatar image AlwaysSunny JustJunuh · Dec 27, 2016 at 06:40 AM 1
Share

In your case they were short snippets so that's fine, but for multi-line code with different indentation levels, you should always paste the code, highlight it, and click the 101010 button in the web form toolbar. That'll format it like so:

 This is some code

Glad to help - I remember first grasping the power of this concept. It's a useful approach for solving many kinds of problems! Best,

avatar image JustJunuh AlwaysSunny · Dec 27, 2016 at 07:35 AM 0
Share

Got it. Yeah I just noticed that 10101 button.

 Woops
 

So seems like I'm good to go. The only problem I'm having is figuring a good way to constrain the rotations so that the player can't look too far overhead or too far below the character. I built the camera system exactly as you said.

I tried something like:

   if (transform.eulerAngles.x > maxHeight)
    {
         transform.rotation = new Quaternion(maxHeight,transform.rotation.y,transform.rotation.z,transform.rotation.w);
    }
    if (transform.eulerAngles.x < $$anonymous$$Height)
    {
            transform.rotation = new Quaternion($$anonymous$$Height,transform.rotation.y,transform.rotation.z,transform.rotation.w);
    }

But to no such luck. Seems I have a problem.

The rotation of the main empty camera rig (the one glued to the player but not childed) gets very skewed to the point where I can't rely on a single rotation value (by rotation value I mean x,y,z,w I don't know what you would call those individually) to measure a $$anonymous$$ and max value for the camera.

So, how should I measure a $$anonymous$$ and max height for the camera system?

Show more comments
Show more comments
avatar image AlwaysSunny JustJunuh · Dec 27, 2016 at 11:28 AM 0
Share

In response to latest post - it only lets you "reply" so deep...
You did your own debugging + research - I wish all newbies were just like you! :)

You are - perhaps for the first time - encountering the shortco$$anonymous$$gs of Euler representation. This phenomenon (often called "gimbal lock" although technically that's something more specific) is why Quaternions are used internally for processing rotations.

The gist is this: Unless you also $$anonymous$$ch it how, almost anything dealing with Euler angles in Unity doesn't care about the sign of angles, and doesn't consider angles greater than 180 degrees or less than zero degrees.

It requires some mental gymnastics every time this comes up, but there is always a relatively painless way to overcome this quirk. Eventually you'll appreciate the elegance of the workarounds more than the lost convenience of Euler, though I must admit it will take some time.

The Quaternion class offers several mathematical operations and methods which help programmers overcome their un-intuitive nature. Have a glance over their API reference to get an idea. Unless you know precisely what you're doing, you shouldn't mess with the components (.x .y .z .w) of a Quaternion - they don't mean anything automatically useful to a human brain.

For that reason, it's frequently more intuitive to convert them to Euler angles, perform human-friendly math on those angles, then convert those Euler angles back into Quaternion space (which is what we just did). But you will definitely run into this situation again down the road, where that procedure creates a similar problem. Next time you'll recognize it right away.

The first thing to try is avoiding the problem. Outside of playmode in the editor, detatch the grandchild camera target from the hinge. Rotate the hinge 90 degrees along X, then re-attach the grandchild. Change the $$anonymous$$ / max angles to values between greater-than-zero and less-than-180.

I recommend putting the camera target grandchild at a local position such as ( 0, 0, 10 ) so the line between the camera target's "resting position" and its parent is parallel to the floor.

We do all this to make the "halfway point" through your envelope also be the halfway point through the Euler representation's useful range. (So with the hinge at 90 degrees, the camera will be "straight out" and looking at the player straight on, you'll have 90 degrees of freedom up or down without worrying about gimbal lock.)

If this works for your situation, perfect! (I think it should.) If not, we'll try opening that can o' worms, but beyond here there be dragons.

avatar image JustJunuh AlwaysSunny · Dec 27, 2016 at 11:07 PM 0
Share

Took some tuning but I finally got it! Works smooth and everything is seamless! No jitters or anything!

I'm gonna have to call this long conversation a success! $$anonymous$$y next goal is to get a bit of a deacceleration with the camera movements so it doesn't whip around so quickly and some motion blur.

After all your help it's hard for me to let you go :c You've proven to extremely helpful! Thanks for everything! This won't be my last question so I'll see you around!

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.

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

7 People are following this question.

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

Related Questions

FPS cam for Roll -a-Ball like game 1 Answer

How can i make my camera rotate/move relative to my players rotation? 0 Answers

How would I make a camera work like the one in ROBLOX? 0 Answers

Can you offset the dead zone of Cinemachine's camera from the center? 0 Answers

Make a camera to Follow two players 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