Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 UY505 · Jun 23, 2015 at 12:41 PM · c#camerajavascript

Rotate camera around ball; always face "back" of ball?

I'm trying to get my camera to rotate around my ball when I turn left and right. I'm using the Update method and:

 void Update () {
     //transform.eulerAngles = new Vector3 (1, 1, 0);
     transform.RotateAround(player.transform.position, player.transform.eulerAngles, 0);

 }

but the camera just goes all spastic. How can I keep my camera facing the back of my ball as if it were a person and we were always behind the person who is walking?

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 Hellium · Jun 23, 2015 at 09:59 PM 0
Share

Why don't you simply put your camera as a child of your ball ? Thus, when your ball will turn, the camera will follow the ball and stay behind it.

By the way, in the standard assets, there is a Character Controller which is based on this principle (not sure)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Baste · Jun 23, 2015 at 10:36 PM

RotateAround rotates your transform around a point, with regards to an axis, a number of degrees. Read the docs here, there's a good example.

If you want to keep the camera at a fixed position and rotation with regards to the player character, @Hellium's comment is the best bet - make the camera a child of the player, and skip the scripting. If that's not viable, this is a super-cheap implementation:

 float cameraVDistance = 10f; //how far the camera is behind the player
 float cameraHDistance = 5f; //how far the camera is above the player
 
 void Update() {
     Vector3 playerPosition = player.transform.position;

     Vector3 behindPlayer = -player.transform.forward * cameraVDistance;
     Vector3 abovePlayer = Vector3.up * cameraHDistance;

     Vector3 cameraPosition = playerPosition + behindPlayer + abovePlayer;

     Vector3 cameraToPlayer = playerPosition - cameraPosition;
     Quaternion cameraRotation = Quaternion.LookRotation(cameraToPlayer);

     transform.position = cameraPosition;
     transform.rotation = cameraRotation;
 }

That's put the camera behind the player, looking at the player. You'll want to mess with the values somewhat, and the camera should probably be looking at a point a bit above the player (so you don't just see mostly ground), but that's a quick start.

Honestly, childing the camera to the player character is easier, but there you go.

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 UY505 · Jun 24, 2015 at 12:00 AM 0
Share

Thank you, i will try this out now. I have childed my cam with my player but it doesn't work that way. I don't know how to explain it, but I'll see how this goes. Thanks again

avatar image UY505 · Jun 24, 2015 at 12:09 AM 0
Share

@Baste I've trying to figure out why this code is rotating the camera forwards over and underneath the ball even when I'm not turning left or right. That's the issue I'm having in the first place. I set my cam as child, but still getting the same result.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Visibility of objects 2 Answers

How do I make a Game Object invisible? 1 Answer

Camera Effect + Translating US to C# 3 Answers

Multiple Cars not working 1 Answer

I want to put main camera floating 0 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