Does anyone have a good suggestion for making the car movement feel a bit more natural by decelerating gradually when you let go of the drive key? (or accelerating when first moving).
Thanks!
Does anyone have a good suggestion for making the car movement feel a bit more natural by decelerating gradually when you let go of the drive key? (or accelerating when first moving).
Thanks!
Hi Samsuma,
Welcome to our community!
You could try to override the value of drag
in the player’s Rigidbody2D component.
https://docs.unity3d.com/ScriptReference/Rigidbody2D-drag.html
With if-statements, you could check GetKeyDown and GetKeyUp and set the value of drag
to, for example, 1f and 10f respectively.
Let me know if this approached worked for you.
See also:
Hi Nina, thanks for the tip. I also thought this, and tried changing the linear and and angular drag settings, but they have no effect on the car (I also tried this on a Box obstacle to get it to “slide” after the car hits it, but with no success).
From what I could gather from reading sources online, it’s because we’re moving the transform position “manually” issuing code, instead of using a force to move the car.
What do you think?
Ah, yes, I think you might be right. By manipulating the transform.position
, we do not affect the velocity
. The drag
affects the velocity
, though.
If you want to challenge yourself, replace the current code for movement. It’s not difficult.
//
in front of the lines.Time.deltaTime
in FixedUpdate.Theoretically, that should work. Unfortunately, there is no code example for the AddRelativeForce method but you could take a look at the AddForce method.
Good luck, and let me know if/how it worked.
Thanks Nina, great advice thank you. I played around with it (for longer than i care to admit and managed to get it working in a way that is a lot more fun (in an arcadey way) to control.
My code here:
void Update()
{
turnAmount = Input.GetAxis("Horizontal");
forwardAmount = Input.GetButton("Fire1");
reverseAmount = Input.GetButton("Fire2");
}
void FixedUpdate()
{
if (forwardAmount && reverseAmount == true)
return;
if (forwardAmount == true)
{
rigidbody2d.AddRelativeForce(Vector2.up * moveSpeed);
}
// moveSpeed is slower when reversing - to keep turning cirlce the same, turnSpeed is also reduced by the same amount.
if (reverseAmount == true)
{
rigidbody2d.transform.Rotate(0, 0, turnSpeed * 0.3f * turnAmount * Time.deltaTime);
rigidbody2d.AddRelativeForce(Vector2.down * moveSpeed * 0.3f);
}
Good job! I like that you processed the user input in Update instead of opting for the simpler (but not that optimal) approach in FixedUpdate.
A little tip: == true
is not necessary unless you like it this way, for example, if (forwardAmount && reverseAmount)
and if (forwardAmount)
would be sufficient. That’s a matter of personal preference, though.
And remove Time.deltaTime
from the FixedAmount method because that might give you laggy results even with transform.Rotate
. FixedUpdate gets executed every x seconds, where x is constant. For this reason, we are not dependent on the framerate in FixedUpdate.
Apart from these two details, I do not see any issues. Keep it the good work!
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.