- Home /
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 )
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 :
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);
}
}
Really, don't use Cos and Sin, that's needlessly complicated; just use Translate. Also use Update, not FixedUpdate, which is only for physics.
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);
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.
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.
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
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.
@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