Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 SSpecks · Dec 06, 2019 at 07:22 PM · rotationrotateballmovingtranslation

How to rotate the camera around an object

I want the camera to rotate around a ball, while the ball can move up and down and such. So like if the ball were to take a turn, then the camera would turn around with it. My code was: public class CameraController : MonoBehaviour {

     public GameObject player;
 
     private Vector3 offset;
 
     public float rotateSpeed = 3.0F;
     void Start()
     {
         offset = transform.position - player.transform.position;
     }
     void LateUpdate ()
     {
         transform.position = player.transform.position + offset;
         transform.Rotate(0, Input.GetAxis("Horizontal"), 0);
 
     }
 }
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

2 Replies

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

Answer by Ermiq · Dec 09, 2019 at 09:59 AM

What do you actually want? Do you want camera to just repeat the balls rotations so it will always rotate around the ball while the ball will be rotated around its axes? In this case, if the ball is controlled by physics, it will give you a crazy camera that will spin around the ball while the ball is spinning along the ground/terrain.


Or maybe you control the ball rotation manually and you want the camera to follow the ball rotations?

In this case you'll need to do the following:

You'll need to update the offset in Update(). Because if you set the offset just in the Start() method, the vector will be pointing out at the same direction in world coordinates system as it has been pointing at the Start(). And using the same vector as an offset, you actually tell the camera not to turn around the ball.

Also, you need to make the offset dependent on the ball rotation. So, when the ball rotates, the offset vector would point out from the ball in some direction relative to the ball current rotation.

For example, you'll need to preset the desired distance between the camera and the ball (e.g., offsetDistance = 2f ) and use the transform's forward property which represents the transform's current forward direction (the direction the object is looking at).

 void Update() {
     Vector3 positionForCamera = player.transform.position - player.transform.forward * offsetDistance;
 }

This will give you a position that will always stay behind the player/ball's back on the desired distance. And when the player/ball will rotate, its forward vector will change too, therefore the positionForCamera will change too, because it depends on the player/ball's forward direction vector.

UPDATE (forgot about rotation):

And then you need to rotate the camera to look at the ball:

 void LateUpdate() {
     //set camera position
     transform.position = positionForCamera;
     //set camera rotation
     transform.rotation = Quaternion.LookRotation(player.transform.position - transform.position, Vector3.up);
 }
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 SSpecks · Dec 10, 2019 at 06:30 PM 0
Share

@ermiq I did exactly what you said, but the positionForCamera is never 'resolved', and never seems to see that it is made in Update(). public class CameraController : $$anonymous$$onoBehaviour {

     public GameObject player;
 
     private Vector3 offset;
     
      public float   offsetDistance = 2f;
 
     public float rotateSpeed = 3.0F;
     void Start()
     {
         offset = transform.position - player.transform.position;
     }
 
     void Update()
     { 
         Vector3 positionForCamera = player.transform.position - player.transform.forward * offsetDistance;
     }
     void LateUpdate ()
     {
        
             //set camera position
             transform.position = positionForCamera;
             //set camera rotation
             transform.rotation = Quaternion.LookRotation(player.transform.position - transform.position, Vector3.up);
             // transform.position = player.transform.position + offset;
        // transform.Rotate(0, Input.GetAxis("Horizontal"), 0);
 
     }
 }

I also keep getting the error: UnassignedReferenceException: The variable of CameraController has not been assigned. --It is talking about the positionForCamera

avatar image Ermiq SSpecks · Dec 10, 2019 at 06:54 PM 1
Share

$$anonymous$$y code was a pseudo-code that should have been adjusted a bit to actually work in your setup.

Particularly, you should have made positionForCamera a global variable to use it in both Update() and LateUpdate(). If you have done that, you wouldn't get the unassigned exception.

So, put the declaration of the positionForCamera outside of methods to make it assessible from any method within the class:

 Vector3 positionForCamera;
 
 void Update() {
    positionForCamera = player.transform.position - player.transform.forward * offsetDistance;
 }

Unity doesn't change the camera position in your current code exactly because of the error. LateUpdate() doesn't know what is the positionForCamera and it doen't assing the camera position to this variable because it's incapsulated within the Update() and is available only within the Update(). To make the LateUpdate() know about this variable you need to make it global (declared somewhere outside of method scopes).

avatar image
0

Answer by $$anonymous$$ · Dec 09, 2019 at 12:37 PM

if you want camera to follow ball just make the camera child of ball gameobject and it will move and rotate with it.

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

155 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 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 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 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 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

Translate and rotate at the sametime problem 1 Answer

Moving and rotating a ball 0 Answers

Rotate a moving object 0 Answers

Wrong rotation while moving 1 Answer

Making a ball roll and keeping the axis? 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