• 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
4
Question by Vexing · Dec 20, 2013 at 07:38 PM · positionplayerrotatefollow

Camera rotation around player while following.

I'm trying to achieve a camera that follows and rotates around the player while allowing the player to do things like rotate in C#. This means that the player and camera need to rotate separately, but I find it impossible to get the camera to rotate if I set it's position to follow the player. I know exactly what the problem with my code is, but I have no idea how to fix it! Please help!

     public float turnSpeed = 0.40f;
     public Transform player;
 
     void Update()
     {
         transform.position = new Vector3(player.position.x, player.position.y + 8.0f, player.position.z + 7.0f);
 
         transform.LookAt(player.position);
 
         transform.RotateAround(player.transform.position, Vector3.up, Input.GetAxis("Mouse X") * turnSpeed);
     }
 
Comment
Add comment · Show 1
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 robertbu · Dec 20, 2013 at 07:41 PM 0
Share

The behavior sounds exactly like what you get with the standard $$anonymous$$ouseOrbit script provided by Unity.

6 Replies

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

Answer by robertbu · Dec 21, 2013 at 02:35 AM

Here are a couple of changes that give what I think you want:

 using UnityEngine;
 using System.Collections;
 
 public class Orbit : MonoBehaviour {
 
     public float turnSpeed = 4.0f;
     public Transform player;
 
     private Vector3 offset;
 
     void Start () {
         offset = new Vector3(player.position.x, player.position.y + 8.0f, player.position.z + 7.0f);
     }
 
     void LateUpdate()
     {
         offset = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
         transform.position = player.position + offset; 
         transform.LookAt(player.position);
     }
 }
Comment
Add comment · Show 6 · 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 gulfam131 · Nov 20, 2014 at 05:51 AM 0
Share

Thanks a lot, it helps me. .. .

avatar image franco319 · Sep 29, 2017 at 10:32 PM 0
Share

Hello @robertbu please can you tell me how to zoom the camera or make the orbit nearer to the player?

avatar image hyadosm2006 · May 27, 2018 at 12:07 AM 0
Share

offset = new Vector3(player.position.x, player.position.y + 8.0f, player.position.z + 7.0f); change to offset = new Vector3(10,10,10); that is the zoom

avatar image samsunght5670 · Feb 17, 2020 at 08:18 PM 0
Share

$$anonymous$$ay you explain one thing to me, please? Why does quaternion in line 17 after being multiplied by vector3 becomes vector3? how does it work?

avatar image Hellium samsunght5670 · Feb 17, 2020 at 08:22 PM 0
Share
  • https://answers.unity.com/questions/372371/multiply-quaternion-by-vector3-how-is-done.html

  • https://answers.unity.com/questions/186252/multiply-quaternion-by-vector.html

  • https://stackoverflow.com/questions/58071118/how-to-multiply-a-quaternion-with-a-vector

  • https://en.m.wikipedia.org/wiki/Euler%E2%80%93Rodrigues_formula

avatar image Thrasher96x · May 11 at 06:53 PM 0
Share

Finally I found what I was looking for. I had the same problem and this works brilliantly, not even needed to adapt the code that much. That linecode (#17) makes the difference. I struggled for a long time to make a decent third person camera, and now I have that, thanks a lot for your contribution.

avatar image
4

Answer by MrBalin · Sep 29, 2014 at 01:26 AM

HO HOHOHO Here's how to get the camera to rotate around the X axis or rather over and under the player.

 using UnityEngine;
 using System.Collections;
 
 public class Orbit : MonoBehaviour {
 
     public float turnSpeed = 4.0f;
     public Transform player;
 
     public float height = 1f;
     public float distance = 2f;
     
     private Vector3 offsetX;
     private Vector3 offsetY;
     
     void Start () {
 
         offsetX = new Vector3 (0, height, distance);
         offsetY = new Vector3 (0, 0, distance);
     }
     
     void LateUpdate()
     {
         offsetX = Quaternion.AngleAxis (Input.GetAxis("Mouse Y") * turnSpeed, Vector3.up) * offsetX;
         offsetY = Quaternion.AngleAxis (Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offsetY;
         transform.position = player.position + offsetX; 
         transform.LookAt(player.position);
     }
 }
 
Comment
Add comment · Show 3 · 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 OmegaNemesis28 · Jan 11, 2015 at 06:36 AM 0
Share

So what did you do with offsetY exactly? I dont see it being used besides Quaternion.AngleAxis

avatar image BananaClipStudio · Jul 13, 2018 at 02:49 AM 0
Share

He also used "$$anonymous$$ouse Y" for both the x and the Y axis. IF you are gonna help don't mess with people. It is best to just not help at all if you are. All this is going to do is frustrate new developers

avatar image steryo · Jan 28, 2019 at 03:05 PM 0
Share

worked like a charm, thanks a lot

avatar image
3

Answer by alex13243 · Sep 01, 2015 at 11:08 AM

i used it too you need this modification of that

public float turnSpeed = 10f; public Transform player;

 public float height = 1f;
     public float distance = 2f;
 
 private Vector3 offsetX;
 private Vector3 offsetY;
 
  void Start () {
     
     offsetX = new Vector3 (0, height, distance);
     offsetY = new Vector3 (0, 0, distance);
 }
      
     void LateUpdate()
 {
     offsetX = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offsetX;
     offsetY = Quaternion.AngleAxis (Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offsetY;
     transform.position = player.position + offsetX + offsetY;
     transform.LookAt(player.position);
 }
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 MrBalin · Sep 29, 2014 at 01:27 AM

I tried your suggestion but edited to fit my needs, rotates camera around public Transform variable "player", wherever that player object may be. This only rotates the camera around the x axis of the player's position. How can I raise the camera to slightly above the player's position?
Any suggestions on rotation on the Y axis?

 using UnityEngine;
 using System.Collections;
 
 public class Orbit : MonoBehaviour {
     
     public float turnSpeed = 4.0f;
     public Transform player;
     
     public float height = 1f;
     public float distance = 2f;
     
     private Vector3 offsetX;
     
     void Start () {
         
         offsetX = new Vector3(0, height, distance);
     }
     
     void LateUpdate()
     {
         offsetX = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offsetX;
         transform.position = player.position + offsetX; 
         transform.LookAt(player.position);
     }
 }
Comment
Add comment · Show 1 · 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 Nathanaji01 · Jun 20, 2019 at 07:58 AM 0
Share

Thank you so much this is exactly what I was looking for.

avatar image
0

Answer by koopa · Aug 03, 2015 at 04:06 AM

@Omega MrBalins code is perfect he just left out part of line 25 so ppl couldn't copy and paste his work

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.

Follow this Question

Answers Answers and Comments

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

Related Questions

How to keep position on input.get axis even when it is pressed twice? 1 Answer

Make game object face player and at the same time follow mouse position 2 Answers

Rotate against player when hit 1 Answer

Rotate to face player, drunk issues 0 Answers

Camera Rotate to mouse position -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