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 Rocket35 · Nov 18, 2015 at 12:54 AM · c#2dmove an objectarrow-keys

How do I move a game object only on the x axis with arrow keys?

I am new to Unity and trying to make a space invaders game . I don't know how to move a game object with just arrow keys only on the x axis. I prefer to code in C# but JS would be fine.

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

Answer by NinjaISV · Nov 18, 2015 at 01:52 AM

I'm assuming you're making a 2D game because you only want the object to move on the x axis, so the first thing you'll need to do is add a Rigidbody2D to the object you're planning on moving. Next set it's "Gravity Scale" to 0 and constrain its angle on the z axis so it doesn't spin around randomly. alt text Now just make a class with this code in it and attach it to the object you would like to move around. You can also change how fast it moves by modifying the "Speed' variable in the inspector.

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
     public float speed = 3f;
     Rigidbody2D rigidbody2d;
 
     void Awake () {
         rigidbody2d = GetComponent<Rigidbody2D> ();
     }
 
     void FixedUpdate () {
         if (Input.GetKey (KeyCode.LeftArrow))
             rigidbody2d.velocity = new Vector2 (speed * -1, 0f);
         if (Input.GetKey (KeyCode.RightArrow))
             rigidbody2d.velocity = new Vector2 (speed, 0f);
     }
 }

All this code is doing is getting the Rigidbody2D at the very beginning then assigning its velocity when the player presses the left and right keys.


rigidbody2d.png (17.0 kB)
Comment
Add comment · Show 2 · 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 Rocket35 · Nov 18, 2015 at 08:53 PM 1
Share

@NinjaISV Thank you so much that really helped

avatar image NinjaISV Rocket35 · Nov 18, 2015 at 09:11 PM 0
Share

Awesome! Glad it helped you.

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

Moving moving Object with left/right arrow keys in a circular direction 3 Answers

How do you compare multiple parts of the same array? 0 Answers

converting degrees per second into speed 1 Answer

How do I change the image of an SVG sprite? 0 Answers

Boss ai help EoW 0 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