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 KaeTheDev · Apr 04, 2014 at 03:18 PM · input.getaxisy-axispongx-axis

Making a Paddle Move Vertically?

I'm trying to recreate the Pong game as a practice exercise. Right now, i'm trying to get the paddle to move up and down like the following:

alt text

This is the code that I have:

using UnityEngine;

using System.Collections;

public class Player : MonoBehaviour {

public int Speed = 10;

void Update () {

Vector3 movement = new Vector3(Input.GetAxis("Vertical") * Speed,0,0);

this.transform.Translate(movement * Time.deltaTime);

 }

}

But when I press the up/down arrow keys, this code makes the bar move from left to right.

alt text

How would I get it to move along the Y-axis and not the x-axis? Does it matter if I'm making a 2D or 3D game?

Thank you

pong.png (4.4 kB)
pong2.png (5.7 kB)
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 robertbu · Apr 04, 2014 at 03:32 PM 2
Share

I modifying code, it help to first understand what the current code does. Unity's movement is based on a Vector3. A Vector3 has an x, a y and a z. You create a new Vector3 by Vector3(x,y,z).

If you take a look at your current code, it is doing this:

 new Vector3(movement, 0, 0)

That is it is being passed three parameters. The calculation 'Input.GetAxis("Vertical") * Speed' is being passed as the first parameter, and 0 is being passed for the second and third parameters. So the movement will be in the 'x' direction, since the calculation is the first parameter. $$anonymous$$ake it the second parameter, and you will get 'y' movement.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KaeTheDev · Apr 05, 2014 at 01:36 AM

Robertbu,

Thank you for your help.

I changed the line to: Vector3 movement = new Vector3(0,Input.GetAxis("Vertical") * Speed,0);

And now instead of moving from left to right it moves inward and outward like its moving on the z axis.

If I'm making it as a basic 2D game should I be using Vector2? or does it matter?

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 robertbu · Apr 05, 2014 at 01:40 AM 0
Share

You need to look at the orientation of your camera. A normal 2D setup has the camera at (0,0,-10) with a rotation (0,0,0). With this setup, your Vector3 calculation would work. I imagine if the object is moving in and out, you have the camera looking down the 'y' axis. If this is the case, then your movement would be on the 'z' axis:

 Vector3 movement = new Vector3(0,0,Input.GetAxis("Vertical") * Speed);
avatar image KaeTheDev · Apr 05, 2014 at 11:59 PM 0
Share

Oh thank you so much! I understand my error now. Its working. I appreciate it.

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

20 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

Related Questions

up and down detect 1 Answer

I have a serious problem! 1 Answer

Enemy Follow Disable Y Axis 1 Answer

LookRotation freaks out.. 2 Answers

smooth follow only on X and Y 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