Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
-1
Question by Nightmare_Games · Apr 28, 2017 at 05:43 AM · beginnercamera movementkeyboard input

Keyboard Input + Camera Movement

I've written a script to move the camera based on keyboard input, which I pieced together from several parts of the Roll a Ball tutorial. However, when I release the key, it takes a fraction of a second for the movement to stop. Obviously, that's not optimal.

 void Update ()
   {
   float horizontal = Input.GetAxis ("Pan Horizontal");
   float vertical   = Input.GetAxis ("Pan Vertical");
   float zoom       = Input.GetAxis ("Zoom");
   Vector3 position = transform.position;
 
   if (horizontal < 0) position.x += .1f;
   if (horizontal > 0) position.x -= .1f;
   if (vertical < 0) position.z += .1f;
   if (vertical > 0) position.z -= .1f;
   if (zoom < 0) position.y += .5f;
   if (zoom > 0) position.y -= .5f;
 
   transform.position = position;
   }

Looking online, it seems there are MANY different ways to do this, but most of the examples I've seen have Time.deltaTime somewhere in the math. I thought adding this would fix the problem, but it only seems to have slowed down the movement rather than fix the input.

   if (horizontal < 0) position.x += 10f * Time.deltaTime;

I could easily copy/paste someone else's script, but I'd prefer to understand what's wrong with my script and what parts need to change, rather than simply shove something in without educating myself.

Comment
Add comment · Show 1
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 ShadyProductions · Apr 28, 2017 at 08:51 AM 0
Share

Duplicate question of http://answers.unity3d.com/questions/1345066/input-camera-movement.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Chris_Payne_QS · Apr 28, 2017 at 08:51 AM

Time.deltaTime is necessary to make the code adapt to different framerates - the Update function is called once per frame, but the number of frames completed per second can vary. Using your current code you could move the camera at 10 units per second at 100fps, but only 5 units per second at 50fps.

Your fix for that is correct - multiplying the desired speed by the deltaTime (time elapsed since last frame) - but it may feel slower if your game is running at >100 fps.

Regarding the time delay...I suspect deadzoning. Your camera will only stop moving when the input axes return to exactly zero. Unity's keyboard->axis mapping might have smoothing in it so it takes time to return to 0? A gamepad would not have any smoothing, but analogue sticks (being analogue) are notoriously bad at recentering. Most games define a "deadzone" area in the middle of the axis that is considered "fully upright", like this:

 float DEADZONE = 0.1f;
 if (horizontal < -DEADZONE) position.x += .1f;
 if (horizontal > DEADZONE) position.x -= .1f;

This means the stick doesn't have to be EXACTLY upright to be considered "released".

Comment
Add comment · 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

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

Input + Camera Movement 1 Answer

How do you make a method that gets called when a button gets highlighted using the arrow keys? 0 Answers

2D Character Control Help 0 Answers

Learning Unity For Beginners. 0 Answers

Object doen't move even though i did according lesson 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