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 Aladine · Dec 08, 2013 at 10:32 PM · 2drotationmovementcarforward

2D rotation and moving forward

Hello everyone, i asked this question before and the solution did work pretty good back then, but am opening this subject now because before i was trying to solve a 2d problem in a 3d engine, but now and after the huge 2d update, i want to ask this question again, is it possible to do something like this in unity ?

 -Angle = the object angle
 -Rad   = convert the object angle to randians
 -Move forward :
   object.x += Math.Cos(Rad)
   object.y += Math.Sin(Rad) 


i agree that i have some problems when it comes to rotation, so if any of you have a simpler way to do such a thing, maybe with RigidBody2D then i'll be very grateful (am looking for a 2D top down car movement like this one here )

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Aladine · Dec 08, 2013 at 11:02 PM

SOLVED :D !!! everything worked as i mentioned above, the only thing that i ignored before is the "magnitude" variable, it seems that the whole problem was based on that, because previously i never was able to have the entire angle of an object, it was always a Vector3 that have 3 different values, i thought about return the length of that vecotr, and that's it!! it worked ^_^ !! however, one thing that i still need to do here is to make sure that the "head" of my object it's right side, like this picture : alt text

and here is a simple class to have a simple car movement just like in the link below, i hope it helps :

 using UnityEngine;
 using System.Collections;
 
 public class FollowPath : MonoBehaviour
 {
 
         
         public float speed ;
         public float rotationSpeed;
         //transform
         Transform myTrans;
         //object position
         Vector3 myPos;
         //object rotation
         Vector3 myRot;
         //object rotation 
         float angle;
 
         // Use this for initialization
         void Start ()
         {
                 myTrans = transform;
                 myPos = myTrans.position;
                 myRot = myTrans.rotation.eulerAngles;
         }
     
         // Update is called once per frame
         void FixedUpdate ()
         {
                 
                 //converting the object euler angle's magnitude from to Radians    
                 angle = myTrans.eulerAngles.magnitude * Mathf.Deg2Rad;
 
 
                 //rotate object Right & Left
                 if (Input.GetKey (KeyCode.RightArrow)) {
                         myRot.z -= rotationSpeed;
                 }
                 if (Input.GetKey (KeyCode.LeftArrow)) {
                         myRot.z += rotationSpeed;
                 }
 
 
                 //move object Forward & Backward
                 if (Input.GetKey (KeyCode.UpArrow)) {
                     
                         myPos.x += (Mathf.Cos (angle) * speed) * Time.deltaTime;
                         myPos.y += (Mathf.Sin (angle) * speed) * Time.deltaTime;
                 }
                 if (Input.GetKey (KeyCode.DownArrow)) {
                         myPos.x += Mathf.Cos (angle) * Time.deltaTime;
                         myPos.y += Mathf.Sin (angle) * Time.deltaTime;    
                 }
 
 
                 //Apply
                 myTrans.position = myPos;
                 myTrans.rotation = Quaternion.Euler (myRot);
                 
         }
 }

Comment
Add comment · Show 6 · 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 Eric5h5 · Dec 08, 2013 at 11:06 PM 0
Share

Really, don't use Cos and Sin, that's needlessly complicated; just use Translate. Also use Update, not FixedUpdate, which is only for physics.

avatar image Tjoeker Eric5h5 · Mar 21, 2017 at 02:38 PM 1
Share

How should I implement the rotation and translate? somehow the translate multiplies my rotation by 2 to deter$$anonymous$$e which direction it should go..

my code:

         transform.Translate(transform.up * speed * Time.smoothDeltaTime);
         transform.Rotate(0, 0, Input.GetAxis("Horizontal") * rotateSpeed * -Time.smoothDeltaTime);
 
avatar image Aladine · Dec 09, 2013 at 06:53 PM 0
Share

i want to use Translate but am always working with it "blindly" i don't know what am doing so i just keep trying until it works, but am very familiar with this method, plus it looks pretty simple, straight and the most common algorithm to do such a thing, so why Translate is better ? and yes i agree for using Update ins$$anonymous$$d but in my game i have a lot of physics i just didn't share the entire code, thanx.

avatar image Eric5h5 · Dec 09, 2013 at 09:31 PM 0
Share

Translate is better because it's easier and more flexible, and it makes your code more readable and obvious. Translate and Rotate are pretty trivial to use; maybe you're used to doing things the "hard way" so it takes a $$anonymous$$ute to adjust, but I'd recommend learning to use the Unity API as intended since it will save time and effort in the future. Also it will make things easier for anyone else who might look at your code.

avatar image Aladine · Dec 10, 2013 at 01:14 AM 0
Share

thanx @Eric5h5 and i totally agree with everything that you said, but the reason am doing it this way is to make my codes "general" as possible as i could, so if i want to port the game to another engine, framwork or even from scratch game, i will not spend more time trying to solve how the unity build in functions works, so because this is a learning project i would prefer to keep it this way, however,i may add the translate code to it as a comment, just to remember that i can do it that way, thanx again

avatar image taranggodhari · Aug 14, 2016 at 11:07 AM 0
Share

Thanks mate! You made my day buddy.

avatar image
0

Answer by Eric5h5 · Dec 08, 2013 at 10:46 PM

Use Transform.Rotate and Transform.Translate. You don't need to manually mess with sines and cosines.

Comment
Add comment · Show 1 · 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 Aladine · Dec 08, 2013 at 11:05 PM 0
Share

@Eric5h5 i tried that but i always faced some problems, plus i am used to do it this way when i was developing games with java, flash and now with html5, so the less build-in unity function i use is the better for me

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

19 People are following this question.

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

Related Questions

Rotating an object towards target on a single axis 2 Answers

2d detect 360 (backflip) rotation on z axis? 2 Answers

Top down 2d movement without being affected by rotation 2 Answers

How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D) 3 Answers

Move a 2D character on an uneven ground and also keep moving it in same direction irrespective of the change in rotation 0 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