- Home /
moving a cube
okay, so i literally am a noob, i only got Unity yesterday and have almost NO experience with it. i have a few very basic lines of code for JavaScript but aside from that i have no idea what im doing. Anyway, im trying to make a cube MOVE not ROLL. im using the arrow keys to control the cube but when i press left or right, the cube will begin to roll. Any help will be greatly appreciated
Answer by Berenger · Jun 26, 2012 at 04:45 AM
So, the basics.
Animation is a modifaction of a parameter over time. Here you want the position to change when an input occures.
In Unity, you can create script that inherit from a certain class and attach them on a GameObject. Unity will then call specific functions at a specific times if you define them them in those script. The usual are Awake (All the awake functions are called first when the scene starts), Start (called once all the Awake are done) and Update (once per frame).
You need to modify the position of the cube each frame if the input is pressed. It's going to look like that :
function Update() // The name matter. update() won't work.
{
// By using GetAxis, we use the InputManager. "Horizontal" is q/d or left/right and can be modified.
//float h = Input.GetAxis( "Horizontal" ); // That's C#, silly me
var h : float = Input.GetAxis( "Horizontal" );
// Let's check if the input is pressed. Left is -1, right +1, nothing is 0
if( Mathf.Abs( h ) > 0.0 )
{
// Now, we can move the cube by accessing the Transform component, which contains the matrix
// To make the movement independant from the frame rate, we use Time.deltaTime.
transform.position += Vector3.right * h * Time.deltaTime;
}
}
Answer by Titus16 · Jun 26, 2012 at 11:09 AM
Thankyou for replying BUT.. i typed the code java and it came up with- Assets/Controls.js(8,9): UCE0001: ';' expected. insert a semicolon at the end
i had a quick scan through it and it says that it needs a semicolon in the
float h=Input.Getaxis ("Horizontal");
Again, any help and i will love you forever
Getaxis should be "GetAxis" you missed a capital A
http://unity3d.com/support/documentation/ScriptReference/Input.GetAxis.html
Always check you have typed everything in the right case, it's a mistake that can cause a lot of aggro especially if you are new to program$$anonymous$$g.
It might not be the answer to your current compiler error, but it is an issue that will stop your code from working.
Hope that helps.
It's var h : float = Input.GetAxis( "Horizontal" );. As much as I try, C# always find it's way to come back at me ;)
Your answer
Follow this Question
Related Questions
Everytime ball goes to new plane it bounces 2 Answers
Moving object between two points (script) 1 Answer
Cube Rotating & Moving 1 Answer
How can i stop a WASD Cube Player to move into nothing on a plattform? 2 Answers
Moving and rolling a cube 1 Answer