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 )
SOLVED !!!
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);
}
}