Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by Alsador · Oct 09, 2015 at 08:47 PM · c#movementx-axisarrow-keys

How can I translate an object smoothly on the X Axis only? [C#]

I seem to be having an issue getting a user controlled object to translate along the X axis only via arrow keys. I've tried figuring it out and eventually succeeded but it moves way to fast and I don't know how to slow it down. Sorry for the noobie question.

 public float speed = 50f;
     public float upSpeed = 0.0015f;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
         if(Input.GetKey ("left"))
         {
             transform.position = transform.position + Vector3.left ;
         }
         if(Input.GetKey ("right"))
            {
             transform.position = transform.position + Vector3.right;
         }
     
     }
     void FixedUpdate ()
     {
         GetComponent<Rigidbody2D>().AddForce (Vector3.up * upSpeed);
     }

Any help would be great and much appreciated.

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

Answer by Statement · Oct 09, 2015 at 11:42 PM

Try multiplying the direction with Time.deltaTime, which expresses the elapsed time since last frame in seconds. It can be confusing to get it if you are starting out but it can be helpful to think of its properties like this: If you multiply 1 (for example) by Time.deltaTime, you can think of it being a change of 1 per second.

 transform.position = transform.position + Vector3.left; 

...can be written shorter as...

 transform.position += Vector3.left; 

...can be written cleaner as...

 transform.Translate(Vector3.left);

Translate basically means "move", "offset" or "pan" in this context. And to make your code respect timing in Unity, you should multiply the translated value by Time.deltaTime:

 transform.Translate(Vector3.left * Time.deltaTime);

Vector3.left and Vector3.right are (-1, 0, 0) and (1, 0, 0) respectively, so the multiplication becomes (-1, 0, 0) * Time.deltaTime in the previous code snippet. That is to say that you will now translate left by 1 meter per second. If you had not multiplied by Time.deltaTime, we'd been translating by one meter per frame. That can be both fast or slow, depending on your machine. For example a slow computer might be chugging along at 20 frames per second and a fast computer might be racing at 120 frames per second. The net effect if you had not used Time.deltaTime, on one computer you'd be moving 20 meters per second and on another computer you'd be moving 120 meters per second.

To include your configurable speed into this, we can multiply that into the translate too:

 transform.Translate(Vector3.left * speed * Time.deltaTime);

So the final script could look like this:

 public class Example : MonoBehaviour
 {
     public float speed = 50f;
     public float upSpeed = 0.0015f;
 
     void Update()
     {
         if (Input.GetKey("left"))
         {
             transform.Translate(Vector3.left * speed * Time.deltaTime);
         }
         if (Input.GetKey("right"))
         {
             transform.Translate(Vector3.right * speed * Time.deltaTime);
         }
     }
 
     void FixedUpdate()
     {
         GetComponent<Rigidbody2D>().AddForce(Vector3.up * upSpeed);
     }
 }

But we can reduce the code a little more if you want by using an input axis:

 public class Example : MonoBehaviour
 {
     public float speed = 50f;
     public float upSpeed = 0.0015f;
 
     void Update()
     {
         var direction = new Vector3(Input.GetAxisRaw("Horizontal"), 0, 0);
         transform.Translate(direction * speed * Time.deltaTime);
     }
 
     void FixedUpdate()
     {
         GetComponent<Rigidbody2D>().AddForce(Vector3.up * upSpeed);
     }
 }

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 Alsador · Oct 10, 2015 at 05:49 PM 0
Share

Thanks for the help. For some reason

 var direction = new Vector3(Input.GetAxisRaw("Horizontal"), 0, 0);
         transform.Translate(direction * speed * Time.deltaTime) ;

was only allowing the object along the y Axis , so i simply shifted it over in the the y co ordinate like so

 var directiony = new Vector3(Input.GetAxisRaw("Vertical"), 0, 0);
         transform.Translate(directiony * speed * Time.deltaTime) ;

and it seems to be working just as I wanted it to. Thank you so much for your help and input!!

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

31 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to move model on x axis in increments of 1? Starting from zero at a range of -2 to 2. 2 Answers

How To Make My Player Only Move on the ground? 1 Answer

(Newbie's First Project) Object doesn't stop moving until long after I let go of the button 1 Answer

Regarding transform.position in the roll a ball tutorial 0 Answers

How To Move My Player With Sin And Cos Functions 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