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 AlexanderJBaer · May 01, 2014 at 12:45 AM · camerarotationmousepositionkeys

How do I rotate the camera around the player character with keys A and D while keeping him in the center while using the mouse for facing?

Hi there, I am new to working with Unity and am currently working on a class project.

My current issue is camera control, what I want is to have the camera locked to the character's transforms and leave the camera rotation up to the player using A and D. So far the last few hours spent searching for an answer mostly end up with parenting an empty object to the character and having the camera target the empty object. This results in everything working the way I want except that the character is at the very bottom of the screen, and any offsets I make change the camera pivot. The other popular solution was to parent the camera to the character however that wont work for me since the character's facing is controlled by the mouse position, so when I use this method the camera spins violently whenever it isn't centered.

So far this is what I have for the camera script.

 var character : Transform;
 var height : float = 200;
 var speed : float = 60;
 
 function Update () {
 
 var orb = Input.GetAxis("Horizontal")*speed;
 transform.RotateAround(character.position,Vector3.up,orb*Time.deltaTime);
 
 transform.position.x = character.transform.position.x;
 transform.position.z = character.transform.position.z;
 transform.position.y = character.transform.position.y + height;
 
 }

The camera is supposed to be zoomed out and looking down at a 45-ish degree angle at the character.

Any help is appreciated, I feel like there's something very simple I'm missing. JAVAAAAAAAAAAA!

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
1
Best Answer

Answer by Clet_ · May 01, 2014 at 01:28 AM

Here's a small script that you can attach to your camera.

 using UnityEngine;
 
 public class RotateCamera : MonoBehaviour {
     
     public Transform target;
     public float angularSpeed;
 
     [SerializeField][HideInInspector] 
     private Vector3 initialOffset;
 
     private Vector3 currentOffset;
 
     [ContextMenu("Set Current Offset")]
     private void SetCurrentOffset () {
         if(target == null) {
             return;
         }
 
         initialOffset = transform.position - target.position;
     }
 
     private void Start () {
         if(target == null) {
             Debug.LogError ("Assign a target for the camera in Unity's inspector");
         }
 
         currentOffset = initialOffset;
     }
 
     private void LateUpdate () {
         transform.position = target.position + currentOffset;
 
         float movement = Input.GetAxis ("Horizontal") * angularSpeed * Time.deltaTime;
         if(!Mathf.Approximately (movement, 0f)) {
             transform.RotateAround (target.position, Vector3.up, movement);
             currentOffset = transform.position - target.position;
         }
     }
 }

There is a little bit of preparation for it to works though.

  1. After the script has been attached, drag whatever you want to be followed inside the target field of the script.

  2. Move the camera around in the side and position it as you want it to be in-game.

  3. Click on the small gear symbol on the upper right hand side of the script's inspector and click on "Set Current Offset"

  4. Press play and enjoy

Sorry if the script is in C#, I have absolutely no clue how to code in UnityScript. Hope you can figure it out

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 AlexanderJBaer · May 01, 2014 at 05:04 PM 0
Share

I got it to work! Now I know a little bit more about C#, thank you!

avatar image tbrosh23 · Jul 22, 2015 at 04:41 PM 1
Share

Do you know how I could set this code to work for specific buttons. For example I would like to set the O key to rotate left and the P key to rotate right. I too am a beginner in Unity.

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cinemachine not working with my Movement Script 0 Answers

how do i make first person character rotate left and right along with camera? 0 Answers

Want to rotate camera horizontally around object using left and right arrow keys 0 Answers

move the object where camera look 0 Answers

Input.mousePosition for new networking 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