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 Wolferino · Aug 27, 2014 at 11:14 PM · c#camerarotationsphere

Rotating following camera for a sphere

I'm new to unity and I'm trying to get the hang of it, I'm following this tutorial http://unity3d.com/learn/tutorials/projects/roll-a-ball to try and create movable ball similar to monkey ball. I've created a camera that is following the sphere using this code:

 public class PlayerFollow : MonoBehaviour {
     public GameObject Marble;
     private Vector3 offset;
     // Use this for initialization
     void Start () {
         offset = transform.position;
         Debug.Log (offset);
     
     }
     
     // Update is called once per frame
     void LateUpdate () {
         transform.position = Marble.transform.position + offset;
     }
 }

While this works correctly and follows the sphere, I am unsure how to make it so the camera rotates when the user moves the ball eg. when user moves backwards the camera rotates around to show user the objects behind him.

Also if anyone has any sites to get me started that would be nice of you.

Thanks

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
0

Answer by rutter · Aug 27, 2014 at 11:20 PM

You move and rotate the camera using its transform. I usually find it's simplest to do this by managing two points in space:

  • Where is the camera? (transform.position)

  • What is the camera looking at? (transform.LookAt)

So, in your case, we might do something like this:

  • The camera is located at the player (with some offset)

  • The camera looks in the same direction as the player

In code:

 transform.position = Marble.transform.position + offset;
 transform.LookAt(Marble.transform.position - offset);

You could apply a separate offset, but that should give you the rough idea.

Your next question is probably: how do I get the camera to look around more smoothly? That can get pretty complicated, but in general:

  • Where is the camera looking, right now?

  • Where should the camera be looking, ideally?

During each frame, the first point should move just a little bit closer to the second.

Something like this, maybe:

 //assumes you have two variables, lastLookPoint and targetLookPoint
 Vector3 currentLookPoint = Vector3.MoveTowards(lastLookPoint, targetLookPoint, 5 * Time.deltaTime);
 transform.LookAt(currentLookPoint);
 lastLookPoint = currentLookPoint;

All of these are just suggestions. Ultimately, all you need to do is translate and rotate the camera. You're free to do that in any way you can design and build.

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 Wolferino · Aug 29, 2014 at 06:39 PM 0
Share

I still can't seem to get it working at all, the camera is following the sphere but there's no rotation around when I want to look behind.

I found this link http://code.tutsplus.com/tutorials/unity3d-third-person-cameras--mobile-11230 which sort of helps as it exactly what I want to do but the control is really horrible for a sphere

avatar image ToThinkIsToCreate · Nov 21, 2018 at 09:36 PM 0
Share

I'm in the exact same boat, I am still working on it, like if I leave the first part transform.position = $$anonymous$$arble.transform.position + offset;

in the code the camera does not change at all and just follows from same view. when I take that part out, the camera will use the "lookat" code and actually change direction and rotation but it will not move.... for obvious reasons.

I believe I need to setup "if" statements that detects if player is traveling along the x axis or the z axis and have the camera stay the same offset except from a different angle.

example: marble goes left and camera stays same distance however moves to be behind marble ins$$anonymous$$d of side view

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

Third person follow camera on a sphere 1 Answer

Flip over an object (smooth transition) 3 Answers

Rotation seems to pass through condition checks 1 Answer

I need help with TPS controls! 0 Answers

Is there a way to lock my camera's rotation and movement on certain 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