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 hominen · Dec 09, 2013 at 03:33 PM · move an object

Moving the camera?

I have been attempting to allow the player to move their Camera through wasd. Since I am new to Unity I am kind of in a pickle here so to speak. Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
 
     void Start () {
 
     }
     
     void Update () {
     // To move the Camera get the players key.
         if (string.ReferenceEquals(Input.anyKeyDown, "w")){  
             UnityEngine.GameObject.Main_Camera.Translate(Vector3.up);
             }
         if (string.ReferenceEquals(Input.anyKeyDown, "s")) {
             UnityEngine.GameObject.Main_Camera.Translate(Vector3.down);
 
         }
         if (string.ReferenceEquals(Input.anyKeyDown, "a")) {
             UnityEngine.GameObject.Main_Camera.Translate(Vector3.right);
 
         }
         if (string.ReferenceEquals(Input.anyKeyDown, "d")) {
             UnityEngine.GameObject.Main_Camera.Translate(Vector3.left);
         }
     }
 }

The output is that Main Camera is not a defined object.

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 tanoshimi · Dec 09, 2013 at 03:43 PM

Try this instead - if you want to make the speed of movements framerate-independent (which, generally, you always do), you should always adjust any movements by the amount of time which has passed since the last frame, which is what the * Time.deltaTime term is for :

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
     
     // Update is called once per frame
     void Update () {
     
         if(Input.GetKey(KeyCode.W)){
             Camera.main.transform.Translate(Vector3.up * Time.deltaTime);    
         }
         if(Input.GetKey(KeyCode.S)){
             Camera.main.transform.Translate(Vector3.down * Time.deltaTime);    
         }
         if(Input.GetKey(KeyCode.A)){
             Camera.main.transform.Translate(Vector3.right * Time.deltaTime);    
         }
         if(Input.GetKey(KeyCode.D)){
             Camera.main.transform.Translate(Vector3.left * Time.deltaTime);    
         }
     }
 }
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 hominen · Dec 09, 2013 at 04:27 PM 0
Share

Thanks for the help.

avatar image
0

Answer by robertbu · Dec 09, 2013 at 03:37 PM

Assuming you haven't changed the main camera's tag, you can reference the camera by Camera.main. So to translate you can do:

 Camera.main.transform.Translate(Vector3.up);

Also for capturing your board events consider:

 if (Input.GetKeyDown(KeyCode.A)) 
Comment
Add comment · Show 3 · 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 tanoshimi · Dec 09, 2013 at 03:41 PM 0
Share

Also consider Time.deltaTime ;)

avatar image robertbu · Dec 09, 2013 at 03:51 PM 0
Share

@tanoshimi - his code is looking for a key down and to translate 1 unit when pressed. You wouldn't want to use Time.deltaTime if this is the intended behavior. He may very well want a continuous/smooth move like you've coded, in which case Time.deltaTime is important.

avatar image tanoshimi · Dec 09, 2013 at 04:58 PM 0
Share

@robertbu - Good point. I was assu$$anonymous$$g the intention of the code was to create continuous movement and therefore changed Get$$anonymous$$eyDown-> Get$$anonymous$$ey and added deltaTime, second-guessing that the next question would be "how come it doesn't carry on moving when I hold the key down".... ;)

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

17 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

Related Questions

Replace A Component Then Crashes 0 Answers

Stop a enemy by pointing a light at it. 4 Answers

How to refine the enemy moving towards me? 4 Answers

How can you make it that the FPS can pickup a object and move it around like in most source games 1 Answer

Moving objects problem 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