C
C#13mo 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
HimmDawg13mo 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
NAS2413mo ago
Ok So what do I change it to?
HimmDawg
HimmDawg13mo 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
NAS2413mo ago
It is Unity yes
Accord
Accord13mo 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
More Posts