Hello everybody I’m new to unity, after some basic tutorials i have started to create simple game, I need something like this function in gamemaker: motion_set(dir,speed) Sets the motion with the given speed in direction dir. is there any built in fuction for this mean? if not, can somebody give me some code to set my object moving to specific direction ( angle in degree ) by specific speed? Regards
To move your GameObject you could use the RigidBody Component. After adding it to your GameObject write a code like so:
public Rigidbody2D rb;
public float speed;
void Start()
{
rb = GetComponent<Rigidbody2D>();
speed = 5;
}
void Update()
{
rb.velocity = new Vector2(0,1) * speed * Time.deltaTime;
}
rb.velocity sets the velocity of the rigidbody using a Vector with the direction with the two components x and y. Then you can multiply this vector by a speed.
If you would use an angle instead a Vector you could transform your Angle to a Vector2 like this:
public Rigidbody2D rb;
public float speed;
public float angle;
public Vector2 direction;
void Start()
{
rb = GetComponent<Rigidbody2D>();
speed = 5;
}
void Update()
{
direction = (Vector2)(Quaternion.Euler(0,0,angle) * Vector2.right);
rb.velocity = direction * speed * Time.deltaTime;
}
I highly doubt you followed any tutorials since the answer is in said tutorials. I you want code because you are too lazy, then say so.
I’m sure you didn’t understand what i want, or if you understood that you are lazy to answer me or however giving me a link to that tutorial. It seems you don’t know the right answer of me but you like to show yourself professional too!