Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by shbai · May 01, 2015 at 04:07 AM · c#rotationthrust

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

alt text

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.

capture.png (8.5 kB)
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shbai · May 01, 2015 at 04:33 AM 0
Share

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.

avatar image BMRX · May 01, 2015 at 04:40 AM 0
Share

It would help if we could see everything you are using for movement.

avatar image shbai · May 01, 2015 at 04:52 AM 0
Share

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.

avatar image Spinnernicholas · May 01, 2015 at 04:34 PM 1
Share

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.

avatar image shbai · May 02, 2015 at 01:25 AM 0
Share

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 :)

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ARKMs · May 01, 2015 at 09:34 PM

Use transform.forward to get dirrection. http://docs.unity3d.com/ScriptReference/Transform-forward.html

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Spinnernicholas · May 01, 2015 at 11:49 PM 1
Share

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.

avatar image shbai · May 02, 2015 at 01:25 AM 0
Share

Thanks mate :)

avatar image
1

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);
         }
     }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shbai · May 03, 2015 at 01:52 AM 0
Share

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.

avatar image shbai · May 03, 2015 at 11:57 PM 0
Share

Got it working, thanks :)

avatar image
0

Answer by Malek-Bakeer · May 04, 2015 at 05:30 AM

 transform.position += -transform.forward * Time.deltaTime;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Getting the rotation of an object does not return correct rotation 1 Answer

Is there a way to lock my camera's rotation and movement on certain axis? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges