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 yb1980 · Aug 19, 2014 at 03:49 PM · camera-movementcamera rotate

Camera Controls

Hi all i'm new to development and programming/scripting, and having a few problems. I want to create a simple script for camera movement (left,right, forward, back, rotate and zoom in & out). I've been looking through the question/answers here and the internet for some guidance and have come up with the following script.


 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
     
     int cameraVelocity = 10; 
     
     // Use this for initialization
     void Start () {
         // Initial position set to current active player
     }
     
     // Update is called once per frame
     void update () {
         // Camera Movement - Forward, Back, Left, Right, Rotate, Up & Down
         if((Input.GetKey(KeyCode.W))){
             transform.Translate((Vector3.forward* cameraVelocity) * Time.deltaTime);
         }
         if((Input.GetKey(KeyCode.S))){
             transform.Translate((Vector3.back* cameraVelocity) * Time.deltaTime);
         }
         if((Input.GetKey(KeyCode.A))){
             transform.Translate((Vector3.left* cameraVelocity) * Time.deltaTime);
         }
         if((Input.GetKey(KeyCode.D))){
             transform.Translate((Vector3.right * cameraVelocity) * Time.deltaTime);
         }
 
         // Will change for a zoom on Mouse wheel later
         //if((Input.GetKey(KeyCode.UpArrow))){
         //    transform.Translate((Vector3.up * cameraVelocity) * Time.deltaTime);
         //}
         //if((Input.GetKey(KeyCode.DownArrow))){
         //    transform.Translate((Vector3.down * cameraVelocity) * Time.deltaTime);
         //}
 
         // Rotate Clockwise
         //if(Input.GetKey(KeyCode.Q)){
         //    transform.Rotate((0,90,0 * cameraVelocity) * Time.deltaTime);
         //}
         // Rotate Anti-Clockwise
         //if(Input.GetKeyDown(KeyCode.E)){
         //    transform.Rotate((0,-90,0 * cameraVelocity) * Time.deltaTime);
         //}
     }
 }



This has been attached the MainCamera object, i have currently commented out the up/down & rotate as even the WASD has stopped working, but i've never had the rotate command working. Can anyone assist me on what i'm doing wrong?

At a later date i would like to add commands that will centre the camera behind different player objects with the key presses 1-4 and lock the height of the camera on the Y Axis but first really want to get the standard controls working.

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

Answer by robertbu · Aug 19, 2014 at 03:58 PM

Your big problem is that you've misspelled 'Update()' You have 'update' with a lower case 'u' and it needs to be 'Update' with an upper case 'U'. As for your rotation code, you must pass transform.Rotate a Vector3. You have some numbers and commas there, but you must create a new Vector3 out of them. Here is your code with the two fixes:

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
     
     float cameraVelocity = 10.0f; 
     float rotationSpeed = 1.0f;
     
     // Use this for initialization
     void Start () {
         // Initial position set to current active player
     }
     
     // Update is called once per frame
     void Update () {
         // Camera Movement - Forward, Back, Left, Right, Rotate, Up & Down
         if((Input.GetKey(KeyCode.W))){
             transform.Translate((Vector3.forward* cameraVelocity) * Time.deltaTime);
         }
         if((Input.GetKey(KeyCode.S))){
             transform.Translate((Vector3.back* cameraVelocity) * Time.deltaTime);
         }
         if((Input.GetKey(KeyCode.A))){
             transform.Translate((Vector3.left* cameraVelocity) * Time.deltaTime);
         }
         if((Input.GetKey(KeyCode.D))){
             transform.Translate((Vector3.right * cameraVelocity) * Time.deltaTime);
         }
         
          //Will change for a zoom on Mouse wheel later
         if((Input.GetKey(KeyCode.UpArrow))){
           transform.Translate((Vector3.up * cameraVelocity) * Time.deltaTime);
         }
         if((Input.GetKey(KeyCode.DownArrow))){
           transform.Translate((Vector3.down * cameraVelocity) * Time.deltaTime);
         }
         
          //Rotate Clockwise
         if(Input.GetKey(KeyCode.Q)){
           transform.Rotate(new Vector3(0,90,0) * rotationSpeed * Time.deltaTime);
         }
          //Rotate Anti-Clockwise
         if(Input.GetKey(KeyCode.E)){
           transform.Rotate(new Vector3(0,-90,0) * rotationSpeed * Time.deltaTime);
         }
     }
 }

I made a couple of other small changes. I made 'cameraVelocity' a float, added a 'rotationSpeed', and change GetKeyDown to GetKey.

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 yb1980 · Aug 19, 2014 at 04:23 PM 0
Share

Thanks that's great, completely missed the lowercase "U" in Update.

avatar image
0

Answer by Pecek · Aug 19, 2014 at 04:12 PM

First of all, you shouldn't hardcode the control keys like that, if you use "GetButton" instead of "GetKey" the player can customize it(and it won't give you nightmares if a few weeks later you want to change the controls a bit - if you ever had a game with more than 4 buttons you know what I'm saying).

I don't know c#, but I guess you have to explicitly tell that you want to create a Vector3, I just remade your code in boo(the rotating part) and it works fine.

 def Update ():
         if Input.GetButtonDown("Horizontal"):
             transform.Rotate(Vector3(0, 90, 0) * 10 * Time.deltaTime)

Btw if you are using exactly this code it shouldn't work at all since you are doing the movement in the update function instead of Update.

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 yb1980 · Aug 19, 2014 at 04:25 PM 0
Share

Thanks for the reply, i've done a little investigation and trial and error with using GetButton & GetButtonDown. But can not get it working. If i use either of the above commands the camera only moves in one direction.

         if (Input.GetButton("Vertical")){
             transform.Translate (Vector3.forward * cameraSpeed * Time.deltaTime);
         }
         if(Input.GetButton("Horizontal")){
             transform.Translate (Vector3.left * cameraSpeed * Time.deltaTime);
         }
 
         // Rotate Camera Clockwise & Anti-Clockwise
         if(Input.GetButton("Rotate")){
             transform.Rotate(new Vector3(0,90,0) * cameraRotSpeed * Time.deltaTime);
         }

From what i've read on the forums and others everyone seems to be saying the GetButton command will pickup the Pos & Neg inputs and work in both directions, but i'm only going forward, left and clockwise, with either key. Am i missing something????

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Roll a ball camera with rotating by a mouse? 1 Answer

Camera stucks at the spawn position 1 Answer

Lean/Peak system 1 Answer

Allow both turning of the mouse and turning of the player to keep the camera pointing towards the player 1 Answer

How to orbit camera around player from top-down to sidescroller view? 1 Answer


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