- Home /
Horizontale Movement Won't work !
Hi everyone, i'm making like a Multiplayer PacMan for fun. but i need to make one pacman movement with ''WASD keys'' and one with ''Arrow Keys'' but, i dont know how to move it in horizontal, the verticale work.!
var gravity = 20.0;
var speed = 13;
var jumpSpeed = 8.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
if (grounded) {
if (Input.GetKey (KeyCode.W))
{
moveDirection = Vector3(Input.GetAxis("Vertical"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.GetKey (KeyCode.A))
{
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.GetKey (KeyCode.S))
{
moveDirection = Vector3(Input.GetAxis("Vertical"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.GetKey (KeyCode.D))
{
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
//Gravity
moveDirection.y -= gravity * Time.deltaTime;
//Character
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
hi there i think you should change this like below ( transform.TransformDirection(moveDirection); ) ( transform.Translate(moveDirection); )
Answer by j0ffe · Dec 01, 2012 at 04:00 PM
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
You code should look something like this to work.
moveDirection = Vector3(0, Input.GetAxis("Horizontal"), 0);
moveDirection = transform.TransformDirection(moveDirection) * speed;
You have forgot to put a 0 before your input. A vector3 contains 3 arguments but you only have filled in 2. I also put * speed after just to show that you can have it like that if you want.
All your keykode are now exactly the same. A vector 3 works like this x , y , z. From front x is left and right, y is up and down and z is forward and backward.
EDIT:
At the moment you have
if (Input.GetKey (KeyCode.W))
{
moveDirection = Vector3(Input.GetAxis("Vertical"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
So you put the movement at the x slot so then now then you press W you say that it should move horizontal.
Your should look something like this to work:
if (Input.GetKey (KeyCode.W)) // moves positive on Y-axis (upwards)
{
moveDirection = Vector3(0, Input.GetAxis("Vertical"), 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.GetKey (KeyCode.A)) // moves negative on X-axis
{
moveDirection = Vector3(-Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.GetKey (KeyCode.S)) // moves negative on Y-axis
{
moveDirection = Vector3(0, -Input.GetAxis("Vertical"), 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.GetKey (KeyCode.D)) // moves positive on Z-axis
{
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
Also take notice on that I put a - before on S and A if you want a something to move on the opposite direction. So now from the front. If you press W you will move upwards, if you press S you will move downwards , if you press D you will move left and if you press D you will move to right. If you want it to move forward and backward you simply just need to put it on the Z-axis.
But Now i have this.. and it's my Vertical that wont work.. i need to move it Up to down with "WS keys"
function FixedUpdate() { if (grounded) {
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.W))
{
moveDirection = Vector3(Input.GetAxis("Vertical"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A))
{
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.S))
{
moveDirection = Vector3(Input.GetAxis("Vertical"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.D))
{
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
Yeah but your code, have many character that i dont know what it meen like "
ops must have happend somthing wrong then i copypasted. fixed now
&34; is ". You could tell that if you'd look at the code and compare it to what you have.
The three numbers in a Vector3 are the different axes. You must put Horizontal and Vertical in different positions.
Answer by Benjamin6817 · Dec 01, 2012 at 06:29 AM
I can see your error there... Where you did the horizontal axis you forgot to put a zero before "Input.GetAxis" e.g. instead of
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0);
You would put
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
Notice how I put the zero after that other zero... Your question was some what vague being, you didn't speak of any error messages. But that should work.
[EDIT]: I changed the script, I too made an error here. I positioned the OTHER other zero in the wrong place. Wow that was confusing.
I'm not sure if this was formulated correctly, but there is something to it. Input.GetAxis("Horizontal") results in a single float. Thus
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0);
Is missing an input. As Vector3 contains, x, y, and z.
But how i repair it ? For Vertical i put i need my Vertical and horizontal..
I did'nt have any error message.. i juste need that my character move with WASD keys, without the Arrow in the same time!.
because now when i press W or S my character jump.. and i put youre code..
Answer by ProXenT · Dec 01, 2012 at 09:04 PM
I have that now, but it didnt work wekk.. just show me what i need to modify plz !:(
var gravity = 20.0;
var speed = 13;
var jumpSpeed = 8.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
if (grounded) {
if (Input.GetKey (KeyCode.W)) // moves positive on Y-axis (upwards)
{
moveDirection = Vector3(0, Input.GetAxis("Vertical"), 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.GetKey (KeyCode.A)) // moves negative on X-axis
{
moveDirection = Vector3(-Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.GetKey (KeyCode.S)) // moves negative on Y-axis
{
moveDirection = Vector3(0, -Input.GetAxis("Vertical"), 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.GetKey (KeyCode.D)) // moves positive on Z-axis
{
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
//Gravity
moveDirection.y -= gravity * Time.deltaTime;
//Character
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
What you wrote is not an answer, it is a comment. You need to use the 'add a comment' button if you're adding a comment.
"Didn't work" is unhelpful. Nobody can tell what went wrong if that's all they have to go on.
Vector3 has three axes, and they're always in the same order: (X, Y, Z). Y is for up and down like jumping. You need to apply horizontal movement to the X axis ("side to side"), vertical movement to the Z axis ("forward backward"), and gravity to the Y axis.