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 JustinTheSwift · Feb 10, 2017 at 07:35 PM · jointscamera followrotatearoundcamera movement

Camera that both orbits and follows

I am attempting to make a camera which both follows a player when he moves and orbits the player on key press (think Super Mario 64). I can easily make a camera that follows a player target by setting a fixed offset value and then positioning the camera relative to the player on every update:

 void MoveCamera()
 {
     transform.position = target.position + offset;
 }

I can also easily orbit the player by using transform.rotateAround():

 void Update()
 {
     if (Input.GetKey(KeyCode.LeftArrow))
     {
         MoveHorizontal(true);
     }else if (Input.GetKey(KeyCode.RightArrow))
     {
         MoveHorizontal(false);
     }
 }

 public void MoveHorizontal(bool left)
 {
     float dir = 1;
     if (!left)
     {
         dir *= -1;
     }
     transform.RotateAround(target.position, Vector3.up, horizMove*dir);

     offset = transform.position - target.position;
 }

I cannot do both at the same time, however, because if I use a fixed offset I will break my orbit by always positioning the camera in the same place relative to the player. I have tried to resolve this by changing my offset value each time the camera moves:

 offset = transform.position - target.position;

But for some reason, this doesn't work. Every time I move the camera, it gets farther and farther away from the target, until finally it's hardly looking at him at all.

I'm thinking that there must be some say to set a specific distance from my target without setting a specific position. But I don't know how to go about it. I know I can get the magnitude of my offset, but then what do I do with it?

A few other non-working solutions I've tried:

  1. connecting my camera to my player with a hinge joint. That kinda works, but it also causes some unexpected effects (like the player rotating around with the camera, the camera and player are jittery and jerky, don't follow controls properly etc.) I'm guessing that's not a viable solution, but maybe I just have the settings wrong?

  2. Creating a "camera axis" object directly above my character that moves along with the character and having the camera rotate around that with a 2D distance joint. That behaves essentially just like my original solution.

So how do I do it?

Any solutions to this?

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

Answer by corpsinheretoo · Feb 11, 2017 at 12:49 AM

Instead of doing a physics-based solution for the position of the camera - try simply parenting the camera under your player object in the hierarchy. The camera will automatically follow your player. I tried this using your rotation script and it worked well.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Specific camera coordinates based on players coordinates. 1 Answer

Cinemachine & Playmaker 3rd person zoom 0 Answers

Automatic 3rd person camera 1 Answer

How can I get the camera to follow multiple objects even if some of them are deleted? 1 Answer


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