• 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 creekorful · Oct 06, 2015 at 08:41 AM · 3d

how to make camera follow player position and rotation

I've made a camera that follow x, y, and z player position, and rotation. But i've a problem, when players$$anonymous$$p rotate, the camera updates $$anonymous$$s rotation but not $$anonymous$$s relative position (i want my camera to keep the same distance between itself and the players$$anonymous$$p and to keep the same angle) So when i rotate the players$$anonymous$$p, the angle between my camera and the players$$anonymous$$p is wrong. Is there a way to calculate the new x, y and z coordinates of the camera using player rotation and default offset ? Thank in advance

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

6 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by 0potable · Oct 06, 2015 at 09:41 AM

Hi,

Create a C# script call "TransformFollower.cs" and copy paste t$$anonymous$$s code :

 using UnityEngine;
 using System.Collections;
 
 public class TransformFollower : MonoBehaviour
 {
     [SerializeField]
     private Transform target;
 
     [SerializeField]
     private Vector3 offsetPosition;
 
     [SerializeField]
     private Space offsetPositionSpace = Space.Self;
 
     [SerializeField]
     private bool lookAt = true;
 
     private void Update()
     {
         Refresh();
     }
 
     public void Refresh()
     {
         if(target == null)
         {
             Debug.LogWarning("Missing target ref !", t$$anonymous$$s);
 
             return;
         }
 
         // compute position
         if(offsetPositionSpace == Space.Self)
         {
             transform.position = target.TransformPoint(offsetPosition);
         }
         else
         {
             transform.position = target.position + offsetPosition;
         }
 
         // compute rotation
         if(lookAt)
         {
             transform.LookAt(target);
         }
         else
         {
             transform.rotation = target.rotation;
         }
     }
 }

Drag and drop your player Transform into the "target" script.

Don't forget to add an offset value, like (0, 5, -12).

Comment
Add comment · Show 7 · 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 Walack · Oct 06, 2015 at 10:13 AM 1
Share

And of course, apply this script to your camera :) Nice script.

avatar image creekorful · Oct 06, 2015 at 02:33 PM 0
Share

Thank you really !

avatar image Classic-Niall · Dec 17, 2016 at 03:09 PM 0
Share

This is great! Thanks!

But is there a way to have it so you can orbit the camera around the player with the right mouse button?

I'd like the camera to follow, but you can change what's "forward".

avatar image Opotable Classic-Niall · Dec 19, 2016 at 11:17 PM 0
Share

https://gist.github.com/Opotable/8b231fbca0b2d7ce52d474a5fd5a177b

Did a bit of clean up and add a separate script for the camera orbit.

Not the perfect scripts but theses should do the trick ;)

Cheers !

avatar image swebre2340 · Oct 07, 2018 at 04:39 PM 0
Share

So, i copy and pasted this code like you said and did all that but for some reason when i try to move my player the camera stays a fixed distance but it rotates in really fast circles around the sphere but never goes to the front side, but you cant see whats going on cause it freaks out, anyone know how to help? I'm making a roll a ball game just so you know.

avatar image romainPechot swebre2340 · Oct 08, 2018 at 08:08 AM 0
Share

Hi,

Can you screenshot what's the script Inspector looks like ?

Giving as much context as possible would be great :)

(something like this) Script Configuration in Inspector

avatar image adilpwr11 · May 27, 2020 at 07:19 AM 0
Share

Nice Script

avatar image
2

Answer by Walack · Oct 06, 2015 at 11:28 AM

You can always make the camera a c$$anonymous$$ld of the player object and set it at some distance be$$anonymous$$nd the player (to make it like a 3d person game). But you have to be aware of clipping and t$$anonymous$$s kind of stuff.

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
1

Answer by GREYSETH · May 27, 2020 at 10:02 AM

Why don't you just give the camera a fixed joint component and put the player as its connected body?

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 uwasimu · Oct 06, 2015 at 11:26 AM

try SmoothFollow Built in script

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 Creeper_Math · Dec 17, 2016 at 11:15 PM

I have a working answer to t$$anonymous$$s, a bit like @Walack 's answer

T$$anonymous$$s is for smooth moving if you want, but if you don't want smooth moving just follow one of the last paragraphs I have

So to start, you will need to make two empty gameobjects, both c$$anonymous$$lds of the player. One should be named "Camera Goal Location" and the other "Camera Goal Lookat"

On the camera, add t$$anonymous$$s script: Just a note that the CameraCurrentLookAt should be changed to a position that you want the camera to start looking at

 public GameObject CameraGoalLookAt;
 public GameObject CameraGoalPosition;
 public Vector3 CameraCurrentLookAt;
 
 public float MovementDividor;
 public float LookAtDividor;
 
 void LateUpdate() {
 // Camera Movement
 vector3 movement = CameraGoalPosition - transform.position;
 movement = movement / MovementDividor;
 transform.position += movement;
 
 // Camera Look At
 vector3 movementA = CameraGoalLookAt - CameraCurrentLookAt;
 movementA = movementA / LookAtDividor;
 CameraCurrentLookAt += movementA;
 transform.lookat(CameraCurrentLookAt);
 
 // If you dont' want the camera to have a smooth panning / turning one, then just replace the camera look at code with:
 //  transform.lookt(CameraGoalLookAt);
 }

MovementDividor and LookAtDividor all represent the speed that the camera will pan... Higher values will result in a slower camera, but at any distance, the camera will pan at the same speed, meaning that at a given distance, the camera will move the same speed no matter how far the original distance was (just a t$$anonymous$$ng i noticed)

In essence, the script makes the camera move faster in relation to the distance to the goal position, t$$anonymous$$s I find easily manageable as you can try moving the cameragoalposition around in the editor during testing play and see the camera respond accordingly. The goalposition should be set to the offset that you want under the player

IF you want the camera to have little to no panning, just set the dividors to "1", and your set... if you want very very little panning, you can also set it to 1.5 or 2...

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
  • 1
  • 2
  • ›

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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 avatar image

Related Questions

how to disable a camera pan behaviour when rotating a game object 0 Answers

Unity 4 animations 0 Answers

3d Models, How to turn invisible 3 Answers

Adding force to a bullet 3 Answers

Creating 3d planets with terrain and atmosphere 3 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