- Home /
Moving Cube Forward
Hi,
What I have done so far is made a C# Script called player (player being the cube object) and coded it in the following way:
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
public float PlayerSpeed; void Start() { } // Update is called once per frame void Update () { // Amount to Move float MoveHorizontal = Input.GetAxis("Horizontal") PlayerSpeed Time.deltaTime; float MoveVertical = Input.GetAxis("Vertical") PlayerSpeed Time.deltaTime;
// Move the Player
transform.Translate(Vector3.right * MoveHorizontal);
transform.Rotate(0, PlayerSpeed * MoveVertical, 0);
}
With the code above, hopefully you will see that I did get the cube (Player) moving about the x and y-axis'.
I have visited the Input Manager under Edit->Project Options->Input and made it so I can have the keys that I wanted, for the movement about the vertical and horizontal axis.
I'm wondering now that if their is a z-axis that I am missing under the Input Manager? I have checked around on the net and it seems that what some people are doing is just rotating the position in some way to move forward..
But, so far so good.
Answer by lampshade · May 20, 2010 at 04:28 AM
This is the solution, however, RotateSpeed needs to be tweaked a little, e.g. faster.
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
public float MoveSpeed = 10; public float RotateSpeed = 40;
void Start() { } // Update is called once per frame
void Update () { // Amount to Move float MoveForward = Input.GetAxis("Vertical") MoveSpeed Time.deltaTime; float MoveRotate = Input.GetAxis("Horizontal") RotateSpeed Time.deltaTime;
// Move the player
transform.Translate(Vector3.forward * MoveForward);
transform.Rotate(Vector3.up * MoveRotate);
}
}
Answer by Eric5h5 · May 20, 2010 at 01:26 AM
You can add any number of axes in the input manager and do whatever you like with them, including making a z-axis control.
Thanks for the reply; would you be refferring to the Add Axis feature in the Input $$anonymous$$anager? I have tried adding a '3rd Axis' (Joysticks and ScrollWheel), but I'm not to sure about that.
@lampshade: no, that 3rd axis is the actual 3rd axis on a joystick or gamepad (or it's the scroll wheel on a mouse). Just add another axis in the input manager, and the controls can be whatever you want. Although if your question is about replacing the left/right movement with forward/backward, then change your script so it's using Vector3.forward ins$$anonymous$$d of Vector3.right. The names of the axes in the input manager are merely descriptive, they don't dictate which axis your objects move on. That's up to your code.
Answer by Jon 1 · May 20, 2010 at 02:04 AM
Okay, I work with JavaScript and no nothing about C# so I do not know if this only applies to JavaScript, but the way I do it is this:
if(Input.GetButton("W")) { transform.position = transform.forward PlayerSpeed Time.deltaTime }
This would make the GameObject move forward when the W key is pressed, as in a WASD control scheme. However, like I said, if this is only JavaScript stuff I heartily apologize to you :)
Thanks for the reply, I found exactly what I need to happen at the following link:
http://www.scottpetrovic.com/blog/2009/11/unity3d-3rd-person-basic-movementrotation-wsource/
It's a shame for me that it is written in JS! I've been trying to write it in C#Script..No luck so far
For some reason your the if statement prevents the other lines of code to function properly...And no apology is necessary, I appreciate the reply ;)
Your answer
Follow this Question
Related Questions
How do I launch a object with another object? 0 Answers
Moving game objects 2 Answers
Having trouble moving character backwards... 1 Answer
Truck moves sideways when using transform.forward 1 Answer
Moving character....northwest...? 1 Answer