• 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
Question by Akisato · Dec 09, 2014 at 08:18 AM · c#cameramovementaxis

Keep Camera from player at a certain distance while orbiting around player

So far I have everyt$$anonymous$$ng working perfectly with the following code. The problem is, using transform.forward w$$anonymous$$le keeping distance sets the camera's Y-axis to the same height as the player's Y-axis. I want the camera to be a bit $$anonymous$$gher than the player rather than equal to the player.

The reason I am not simply attac$$anonymous$$ng the camera to the player is because the player is a sphere that is constantly rotating. The method I am using keep the camera separate from the player w$$anonymous$$le still keeping its distance. The player's movement is based on the direction the camera is facing, if that matters at all.

using UnityEngine; using System.Collections;

 public class RotateCamera : MonoBehaviour
 {
     public float rotateSpeed = 300.0f;
     public float distance = 3.0f;
 
     public GameObject player;
 
     // Use this for initialization
     void Start ()
     {
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         // If A key is pressed, rotate camera to the left
         // If D key is pressed, rotate camera to the right
         // if S key is pressed, rotate the camera 180 degrees around player
         if (Input.GetKey(KeyCode.A))
         {
             transform.RotateAround(player.transform.position, Vector3.down, rotateSpeed * Time.deltaTime);
         }
         else if (Input.GetKey(KeyCode.D))
         {
             transform.RotateAround(player.transform.position, Vector3.up, rotateSpeed * Time.deltaTime);
         }
         else if (Input.GetKeyDown(KeyCode.S))
         {
             transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, transform.eulerAngles + 180f * Vector3.up, 100 * Time.deltaTime);
         }
     }
 
     void LateUpdate ()
     {
         // Keep camera a specified distance away from player
         transform.position = player.transform.position - transform.forward * distance;
     }
 }

EDIT: basically I want to combine these two lines of code:

transform.position = player.transform.position - transform.forward * distance;

Combined with

 transform.position = player.transform.position - transform.up * distance;




Comment

People who like this

0 Show 0
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
Best Answer

Answer by NickvanDokkum · Dec 11, 2014 at 08:23 AM

okay, so, have you tried to having a gameobject in the player that dosn't rotate, then putting the camera in that?

Comment
Kramboja

People who like this

1 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 Akisato · Dec 11, 2014 at 01:21 PM 0
Share

If I parent an empty object to the player and set it to a certain height with locked rotation, then the camera doesn't rotate either. I have not tired having an unparanted object that follows the player by script and having the camera follow that empty object. It seems really inefficient though so I was hoping there were other solutions

avatar image

Answer by taxvi · Dec 11, 2014 at 08:47 AM

you should tilt the camera down at the desired angle before $$anonymous$$tting Play button

Comment

People who like this

0 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 Akisato · Dec 11, 2014 at 01:18 PM 0
Share

That's a good idea. I'll try it out. However I think either the code will either tilt the camera back as well or the camera will be pointing at the ground, which isn't that I'm looking for

avatar image taxvi · Dec 11, 2014 at 01:22 PM 0
Share

if this does not work, try calling the transform.LookAt() on line 36, it's very easy to use

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

Making a bubble level (not a game but work tool) 1 Answer

MouseWheel Lerp Smoothing Problem 1 Answer

Arduino with a sensor for Homebrew VR headset 0 Answers

Disable Camera Movement,Stop camera movement 0 Answers

Rotating a player object without rotating the axis 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