Hey Nina! Thank you for the response! I only have a little bit of time this weekend to continue the course but I read over what you mentioned and tried to apply it. I ran into some weird issues though. I think I worked around them, but I am not sure they are in the spirit of what you were describing or not. Or if they are not the recommended way to interact with this stuff.
The first thing I did was create a Vector2d with the inputX and inputY, and passed it into AddRelativeForce. I noticed was that the car wouldn’t rotate but it did move in the direction I would press. To get around that I added back in the transform.Rotate, but I don’t know if that was the intention. It still means that rotation is happening directly on the transform rather than on the rigidbody2d, and in the Update method rather than FixedUpdate.
However, this broke movement, and I figured it was because I was adding a vector of both the horizontal and vertical input as a relative force, and since it is “relative” it was applying it relative to the transform’s rotation (I think?), which was now all over the place. To get around this, my guess was that the vertical axis input was the “target velocity” and that was the only force I needed to add (you mentioned one value should be 0f). Since everything is in 2d, and I was using a Vector2 instead of the Vector3 you mentioned, I passed in the “Movement Speed” * “Y Input(Vertical)” as the Y value, leaving X 0, into the AddRelativeForce function. What I think this resulted in was:
- The transform was rotated at a constant speed in Update like the course originally, which set the direction the car is facing at any given moment
- AddRelativeForce added the velocity in the direction the car was facing during FixedUpdate.
But this lead to another set of issues. In the course version, the car stops when you stop pushing forward on the Vertical axis. By using AddRelativeForce I was noticing that the car never stopped. In fact it would drift around depending on how i added force. I poked around a bit and was curious if adding a “Force” and “Rotating the Transform” was the right choice vs using “SetRotation” and “MovePosition” on the RigidBody.
While trying to figure that out, a friend mentioned that linear drag existed on the RigidBody2D, and so I tried setting that high and it worked (after I updated the move speed), but I am not sure that is the correct way to solve this. I also noticed that even though I was setting the transform rotation, if I collided with something the car would spin ever so slightly and never stop. I tried adding Angular Drag to solve that too and it seemed to.
Is this what you meant? Or was there something I am misunderstanding? SetRotation and MovePosition seem similar to transform’s Rotate and Translate (but not quite the same). I also looked at AddForce instead of AddRelativeForce, but wasn’t sure if I should be using that instead. My guess would be if I did I would need to include the x and y inputs in the vector. But that wouldn’t solve my rotation problem.
Here is the code that I ended up with:
[SerializeField]
float steerSpeed = 300.0f;
[SerializeField]
float moveSpeed = 20.0f;
float inputX;
float inputY;
Rigidbody2D driverRigidBody;
// Start is called before the first frame update
void Start()
{
driverRigidBody = GetComponent<Rigidbody2D>();
driverRigidBody.drag = 6.0f;
driverRigidBody.angularDrag = 20.0f;
}
// Update is called once per frame
void Update()
{
inputX = Input.GetAxis("Horizontal");
inputY = Input.GetAxis("Vertical");
float steerAmount = -1 * inputX * steerSpeed * Time.deltaTime;
transform.Rotate(0, 0, steerAmount);
}
void FixedUpdate()
{
float moveAmount = inputY * moveSpeed;
driverRigidBody.AddRelativeForce(new Vector2(0, moveAmount));
}
Thanks for your help understanding this!