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 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 everything working perfectly with the following code. The problem is, using transform.forward while 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 higher than the player rather than equal to the player.

The reason I am not simply attaching 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 while 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
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
1
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
Add comment · 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
0

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

you should tilt the camera down at the desired angle before hitting Play button

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

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

How to use Android Accelerometer(Left and right tilt) to turn a sphere object and camera in that direction? 1 Answer

Constrain axis of isometric camera ("vertical" only) 0 Answers

Z axis change 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