Beginner B12 – Input with Axes
How to use control with Arrow keys for left and right input axes.
Code Used
//Basic axis input
-
function Update () {
-
var horiz : float = Input.GetAxis("Horizontal");
-
transform.Translate(Vector3(horiz,0,0));
-
}
Related Modules & Challenges
22 Comments
Leave a Comment









Note:
At 3:08 it should be: “Its moving at 1 unit per frame”, not “1 unit per second”. Big difference!
Oops! of course, well spotted. Fixing and reuploading now, thanks Corrie!
could you make a tutorial like this but instead of having the object move on an axis, have it rotate?
thanks
Matt, I may do but you can simply substitute Translate for Rotate – take a look at the Transform class in the script reference. Visit the script reference and search for ‘Transform’ and you’ll see all the things you can do with it via scripting.
@ Beginner B12 – Input with Axes:
How would you do this for the iPhone? Just out of curiosity. The controllers I have are more complex. the ones that come with iPhone unity. When all I want to do is move side to side. This seems perfect.
Thanks
you’ll need to either establish a touch control or make use of the accelerometer to grab a value, assign this value to a variable, then use the value of the variable to move via translate or rigidbody forces in an Update or FixedUpdate function (the latter if you’re using physics).
I got how to move forward too
Here is the code
var speed = 3.0;
var rotateSpeed = 3.0;
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0, Input.GetAxis (“Horizontal”) * rotateSpeed, 0);
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * InputGetAxis (“Vertical”);
controller.SimpleMove(forward * curSpeed);
}
@script RequireComponent(CharacterController)
Hey. These modules are great. thank you. so i see that when you selected ‘jump’ from the input menu, it was attached to the x axis. is that something we’d eventually change to y if we want the player to jump vertically?
Are you referring to this module? doesn’t seem like it.. confused! Either way Jump as an axis is simple a button so its axis doesnt matter as your actual movement or whatever you’re doing is defined axis wise by the code you write, the button just triggers the action.
On the video, you mentioned you had to multiply the value a number lower than one. How do I multiply the value?
transform.Translate(Vector3(horiz,0,0) * 0.1); but more often you’d use transform.Translate(Vector3(horiz,0,0) * Time.deltaTime * speed); where ‘speed’ is a variable you declare and control yourself.
Thank you very much! And this is a wonderful website! I started the week knowing nothing, and ended with a playable game! Kudos to you! ^^
I’m using this to try and control a ball, and after 5 minutes of scratching my head, I realised that as it started moving, it would rotate, and therefore the axis on the object would change and my ball would be trying to fly into the air…
Is there a simple way around this? I’d like to maintain the effect of the ball rolling, but to allow someone to control it with WASD.
Any help would be greatly appreciated.
I’ve kind of solved the problem by:
- Removing the script from ‘Ball’
- Creating a blank object: ‘Ball Handler’
- Placing ‘Ball Handler’ inside of the ‘Ball’
- Making ‘Ball’ the Child of ‘Ball Handler’
- Attaching the script to ‘Ball Handler’
But now I’ve encountered another problem, two actually…
First the ball doesn’t roll realistically, infact it barely rolls at all when it’s running over the ground.
Secondly, and more importantly, when the physical ‘Ball’ hits another object, say, ‘Block’. It makes the ‘Ball’ roll, to which I can’t correct using WASD as I only control the ‘Ball Handler’ and not the ‘Ball’ that’s physically rolling.
Help on either of these would be lovely. And as ever, great work on the tut’s.
Have a look at transformdirection in script ref- you basically need to work in world coordinates rather than local, that’s what’ll be causing majority of your issues.
The object is moving on the z axis, how do I move it in the x axis? using javascript
Use the X value instead of the Z… eg
Translate(5, 0, 0);
values like this are always (X, Y, Z);
This website may be dead, but it’s been a great help so far. I’m thinking about trying to use Unity3D to write a quick 2D platformer and I was going to use horizontal movement learned from this module, but I noticed that if I give my object physics, he doesn’t stay directly on the X-axis, instead kind of shifting back and forth and falling off of my ’2D’ railing. Any quick fix for this other than doing a big solid back/front panel, or is this not the kind of stuff you want to use to create a 2D platformer with a better view.
Hi Nick! its not dead, I still look at stuff, I’m just making something similar and much bigger for Unity directly.
As for restricting – just use Constraints on the Rigidbody component if you use one, or if its a character controller, then consistently set this back to the X position it should be every so often – try not to do it every frame but you should check if it has drifted by a certain amount and set it back. Remember also if you want true 2D you should use orthographic cameras too. Change the Projection setting on your camera.
okay, Im trying to right a script so a cube goes 1 unit forward with one push of the up arrow, 1 back with one push of back arrow, and same for sides. Essentially, I want to write a script for box like the one in http://www.coolmath-games.com/0-briker-2/ . Any clue how? (Im new and suck at scripting)
in a basic sense you could do it like this -
function Update () {
//go right
if(Input.GetKeyUp("d")){
transform.position.x+=1;
}
//go left
else if(Input.GetKeyUp("a")){
transform.position.x-=1;
}
//go up
else if(Input.GetKeyUp("w")){
transform.position.y+=1;
}
//go down
else if(Input.GetKeyUp("s")){
transform.position.y-=1;
}
}
Thnaks, I had to change the y to z, but you helped me alot.