- Home /
Trying to make a game object move in the direction it is facing.
First off, I am completely new to this, so please be patient with me :) So I am making a game (trying to) in which holding the space bar powers up your vehicle and releasing it propels it forward for a certain distance depending on how long you held the button down. I have the ship rotating when I press the arrow keys, but I cant seem to get the ship to move in the direction it is facing.I tried to get the horizontal and vertical axis using this code
float thrust = 1.0f;
//Retrieve axis information
float inputX = Input.GetAxis("Horizontal")* thrust * Time.deltaTime;
float inputY = Input.GetAxis ("Vertical")* thrust * Time.deltaTime;
print (inputY);
print (inputX);
but when I print to the console those values return as the following
Not sure why. Don't really know how much more info you need to know what I mean, so just let me know if I need to be more specific. Thanks in advance.
Just a little more specific info. It is a top down 2d game written in c#, and I am trying to move the object based on its rotation.
It would help if we could see everything you are using for movement.
O$$anonymous$$, so this is what Ive got so far for rotation, i havent got the sprite to move in a smooth line yet though.
float TurnInput()
{
float turnValue = 0;
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))
{
turnValue = -1;
}
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
{
turnValue = 1;
}
return turnValue;
}
public void ApplyRotation(float turnInput)
{
float rotationSpeed = 6;
this.rigidbody2D.AddTorque (rotationSpeed * turnInput);
}
sorry its a bit of a mess. Im still a noob.
Input.GetAxis(...) gets the joystick axis mapped in the Input $$anonymous$$anager.
If that is what you are trying to do. $$anonymous$$ake sure the button mappings are correct.
If ins$$anonymous$$d, you are trying to get the forward direction of the ship in world space, use Transform.forward. Note: you may have to rotate your ship graphics so the front of the ship matches Transform.forward.
Yeah that helped, thanks. sorry, I took so long, working on a lot of stuff. I actually ended up using transform.tanslate.up though. Forward didnt work since it is a 2d game, but it helped me figure it out :)
Answer by ARKMs · May 01, 2015 at 09:34 PM
Use transform.forward to get dirrection. http://docs.unity3d.com/ScriptReference/Transform-forward.html
S$$anonymous$$ling my thunder, I see. That's fine, looks like you need $$anonymous$$arma. He's doesn't seem very focused. I commented that solution 7 hours ago trying to get more information out of him. Hopefully we will hear back soon.
Answer by Jason Ege · May 03, 2015 at 12:09 AM
It is actually a lot easier than that. You can use transform.forward (or rigidbody.forward, in your case) to grab the forward facing direction of the current game object. When using "forward", be sure to set the Space to World or else you will see some weird behavior.
Below is a code sample I wrote for you to get you started. My function uses WASD rather than the space bar, but I think you will get the idea. In my code sample, I use the transform rather than the rigid body, but they should be interchangeable for your purposes.
You will need to place this code inside a class within your script.
float acceleration = 0.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update() {
MoveCharacter();
RotateCharacter();
}
//Gathers input to move the character
void MoveCharacter () {
//If the player presses W
if (Input.GetKey (KeyCode.W))
{
//Increase forward acceleration
if (acceleration < 5.0f)
{
acceleration += 0.1f;
}
}
//If the player presses S
if (Input.GetKey (KeyCode.S))
{
//Accelerate in the reverse direction.
if (acceleration < 5.0f)
{
acceleration -= 0.1f;
}
}
//If the player is not pressing forward or backward.
if (!Input.GetKey (KeyCode.W) && !Input.GetKey (KeyCode.S))
{
//Decrease acceleration.
if (acceleration > 0.0f)
{
acceleration -= 0.1f;
}
}
//Compensate for floating point imprecision.
//If the player is not supposed to be moving, explicitly tell him so.
if (acceleration > -0.05f && acceleration < 0.05f)
{
acceleration = 0.0f;
}
//Move the character in its own forward direction while taking acceleration and time into account.
//The Time.deltaTime maintains consistent speed across all machines by syncing the speed with time.
//Here is where the magic happens.
transform.Translate(transform.forward*acceleration*Time.deltaTime, Space.World);
}
//Gathers input to rotate the character.
void RotateCharacter()
{
//If the player presses D
if (Input.GetKey (KeyCode.D))
{
//Rotate the current game object, using deltaTime for consistency across machines.
transform.Rotate (transform.up, 100.0f*Time.deltaTime, Space.World);
}
//If the player presses A
if (Input.GetKey(KeyCode.A))
{
//Rotate the current game object's transform, using deltaTim for consistency across machines.
transform.Rotate (transform.up, -100.0f*Time.deltaTime, Space.World);
}
}
Wow! thanks man, that helps a ton! :) I wont be able to implement it till tomorrow, but Im pretty sure I get what you mean. I will let you know how I go.
Answer by Malek-Bakeer · May 04, 2015 at 05:30 AM
transform.position += -transform.forward * Time.deltaTime;