- Home /
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:
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.
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
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.
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?
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);
Oh thank you so much! I understand my error now. Its working. I appreciate it.
Your answer
Follow this Question
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