• 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 MegaChuck64 · Apr 14, 2015 at 10:54 PM · cameraballmaze

Need camera to follow rolling balls, without rotating with it, but i still want it to turn.

So im making a 3d maze game, and i want the camera to follow behind the ball, without rotating with the ball, but still being able to turn with the ball, through the maze. The unity rolling ball tutorial only has it follow behind but when it turns the camera stays facing the same way...is there any way to lock the x axis?

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

Answer by shopguy · Apr 15, 2015 at 01:56 AM

Untested and from the top of my head, but I think this would work. Attach script to your camera.

     public Transform ball; // set this to your ball object in editor
     
     void Update()
     {
 // I think this should keep camera position 5 game units behind (negative forward) your ball
     transform.position = ball.position - (ball.forward * 5);
 
 // This should keep camera looking towards your ball
     transform.LookAt(ball);
     }


You probably want it smooth/etc though... I think there are some prefab cameras floating around, "follow camera" or "smooth follow camera" is probably what you want to search for.

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 Halmato · Jul 06, 2016 at 02:24 PM

If you want to be able to control the camera using the mouse, and control the ball's movement with the keyboard keys -

Create a GameObject (CameraRig) with a Camera as a child of the CameraRig, and attach this script to the CameraRig object:

 Transform camTransform;
 public Transform ball;
 public float mouseSensitivityX = 3f;
 public float mouseSensitivityY = 1.5f;
 float verticalLookRotation;
 public float minCamRotationY = -25f;
 public float maxCamRotationY = -5f;

 void Start()
 {
     camTransform = GetComponentInChildren<Camera>().transform;
 }

 // Update is called once per frame
 void Update () {
     transform.position = ball.position;

     transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX);

     verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY;
     verticalLookRotation = Mathf.Clamp(verticalLookRotation, minCamRotationY, maxCamRotationY);
     camTransform.localEulerAngles = Vector3.left * verticalLookRotation;
 }

Position the Camera component as you see fit. Then all you need to do in the ball's movement script is get a reference to the camera's Transform and use its rotation as your relative direction, by changing the ball's movement using

 cameraTransform.TransformDirection(ballMovement)

I hope this works for your situation!

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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

My ball movements are not syncronized with the camera . 2 Answers

How do I make a camera follow an object without changing its rotation in C#? 1 Answer

How to move a ball "faster" looking from a Camera 1 Answer

My game is laggy, but there are only a ball and some flat platforms. 3 Answers

Roll-A-Ball: AddForce into cameras forward direction. How? 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