- Home /
Movin' camera in XYZ
Hi Everyone, i completely new to unity scripting. I´m working on a camera flight through a landscape. Navigating in x and z space by pressing buttons works fine. i used this script:
function Update () {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(x, 0, z);
}
but now i try to move in y direction as well as in z direction. WSAD control x&y. how can i assign other keys to move in y space? thank you for your help dirk
I just formatted your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window =]
Answer by AlucardJay · Sep 13, 2012 at 12:58 PM
You can use the Input Manager :
http://docs.unity3d.com/Documentation/Components/class-InputManager.html
http://docs.unity3d.com/Documentation/Manual/Input.html
or use GetKey :
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetKey.html
function Update () {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
var y : float;
if (Input.GetKey(KeyCode.Q)) {
y = -1.0 * Time.deltaTime * speed;
}
else if (Input.GetKey(KeyCode.E)) {
y = 1.0 * Time.deltaTime * speed;
}
transform.Translate(x, y, z);
}
Answer by adventurepark · Sep 13, 2012 at 01:06 PM
thanks jay kay!
Hi There. No worries, happy if some of this info helped. There is Input $$anonymous$$anager (as alot of people have linked to!) or Get$$anonymous$$ey / Get$$anonymous$$eyDown/Get$$anonymous$$eyUp. I thought I would show an example of Get$$anonymous$$ey just to show something different.
Oh, by the way :
Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.
You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]
Answer by save · Sep 13, 2012 at 01:00 PM
Create a new axis in the Input Manager called "Altitude" with a negative and positive button.
function Update () {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var y = Input.GetAxis("Altitude") * Time.deltaTime * speed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(x, y, z);
}
Answer by electricsauce · Sep 13, 2012 at 01:00 PM
Input.GetButtonDown("Fire1");
assign Fire1 to whatever you like
Answer by hoffmanuel · Sep 13, 2012 at 01:06 PM
Hi!
In your Scenario you are using axis, not keys at the moment. You can add axis or keys via the Input Manager (Edit->Project Settings->Input) You see, Horizontal and Vertical are there defined Already. You Could add an own one by increasing the Size by one and changing the name of the bottommost element to your chosen one.
[Input Manager Reference][1]
Alternately you could use Input.GetKey(KeyCode.[YourKey]) in your code to get your wanted behaviour.
br [1]: http://docs.unity3d.com/Documentation/Components/class-InputManager.html