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 DMCH · Nov 14, 2012 at 07:48 PM · movementtransform.translate

Rookie Problem: transform.translate

Hello,

I'm having some trouble with the code below. I'm trying to set up the vertical movement of a gameObject. The problem is, when I press 'S', the gameobject moves once, then stops. I thought that putting the input check inside the update loop would have it called once per frame, thereby creating continuous movement. Any help would be appreciated!

 using UnityEngine;
 using System.Collections;
 
 public class PlayerPaddle : MonoBehaviour 
 {
     public int speed; 
 
     // Use this for initialization
     void Start () 
     {
         speed = 100;
     }
     
     // Update is called once per frame
     void Update () 
     {
         if(Input.GetKeyDown(KeyCode.S))
         {
             transform.Translate(0, -speed * Time.deltaTime, 0);
         }    
     }
 }
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 chelnok · Nov 14, 2012 at 08:23 PM

You need to change this line:

 if(Input.GetKeyDown(KeyCode.S))

to:

 if(Input.GetKey(KeyCode.S))
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 DMCH · Nov 14, 2012 at 10:33 PM 0
Share

Thanks, I think this most directly addresses the problem!

avatar image chelnok · Nov 15, 2012 at 12:09 AM 0
Share

Indeed. Anyway, Screenhog (and me too) would rather do this with GetAxis. Why? Justbecause.. getaxis / getbutton is kind of extended hardCoded$$anonymous$$ey.S. You can make your own kind of button (or axis) like named $$anonymous$$oveDown. You can set your axis / buttons in input manager. You may allow to player also use pad, joystick, whatever.. and all those be mapped to button($$anonymous$$oveDown) or something like that. It took a while for me to approve the consept, but now when i do, i like it better this way. $$anonymous$$ore info: http://docs.unity3d.com/Documentation/Components/class-Input$$anonymous$$anager.html

avatar image DMCH · Nov 15, 2012 at 12:20 AM 0
Share

Yep, after messing with it, I see what you mean. Thanks again!

avatar image
0

Answer by Screenhog · Nov 14, 2012 at 07:53 PM

You're using GetKeyDown, which "Returns true during the frame the user starts pressing down the key identified by name."

I would instead do this:

 void Update ()
 {
     transform.Translate(0, Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0);
 }

This will use the Horizontal axis to apply movement both upwards and downwards (see Project Settings > Input). The Horizontal Axis automatically uses W for up and S for down (as well as the arrow keys).

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 DMCH · Nov 14, 2012 at 10:34 PM 0
Share

Thanks for the pointer!

Starting to see the merit of this now after fiddling around with it. Very tidy code. Thanks again!

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

11 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

Related Questions

How to get transform.Translate to work with rigidbodies 2 Answers

Trying to stop enemies from clumping together. What am I doing wrong? 2 Answers

Player shaking when colliding with objects 3 Answers

Grid based movement - using transform.translate 3 Answers

transform.Translate vs rigidbody.MovePosition? 3 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