C
C#2y ago
NAS24

❔ Need help trying to make 2D car move from above

// {
if (Input.GetKeyDown(KeyCode.W) == true)
{
myRigidbody.velocity = Vector2.up * 1;
}
if (Input.GetKeyUp(KeyCode.W) == true)
{
myRigidbody.velocity = Vector2.zero;
}
if (Input.GetKeyDown(KeyCode.A) == true)
{
myRigidbody.transform.Rotate(new Vector3(0, 0, 90));
}
}
// {
if (Input.GetKeyDown(KeyCode.W) == true)
{
myRigidbody.velocity = Vector2.up * 1;
}
if (Input.GetKeyUp(KeyCode.W) == true)
{
myRigidbody.velocity = Vector2.zero;
}
if (Input.GetKeyDown(KeyCode.A) == true)
{
myRigidbody.transform.Rotate(new Vector3(0, 0, 90));
}
}
With this code I can only move it up and rotate left 90 degrees. I want to be able of rotating it 90 degrees left and then when I press W it keeps moving in the left direction. Some kind of forward code
5 Replies
HimmDawg
HimmDawg2y ago
You gotta change the directional vector. Which admittedly doesn't directly exist for you right now.
Vector2.up * 1;
^ there it is
Vector2.up * 1;
^ there it is
in 2D, the next position for a movement can be described as x' = x + directionalVector * velocity So when you press A, your directional vector also needs to rotate by 90°
NAS24
NAS24OP2y ago
Ok So what do I change it to?
HimmDawg
HimmDawg2y ago
That's Unity, right? Not sure if there's a more elegant way, but I'd just store a Vector2 in that class and everytime I press A, it rotates by 90°. then, instead of using Vector2.up, you use that new vector
NAS24
NAS24OP2y ago
It is Unity yes
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server