Keep car on the street

In the delivery game how can I force the car motion to only drive on the street and not to go to the green area?

Hi LaraQ,

With the current solution, that’s a bit of a problem because we just move the car without taking the physics simulation into consideration.

With the current solution, you would have to check if the car collided with the street or something else to determine whether the car is allowed to be moved in that direction. The calculation is relatively complex because you don’t want the car to get stuck.

An easier solution would be to use the Rigidbody2D methods (AddRelativeForce). Look the AddRelativeForce of the Rigidbody (not 2D!) up in the API and read the example. The Rigidbody2D method works the same. You have to call it in the FixedUpdate method. For simplicity, you could also check the user input in the FixedUpdate method but, usually, it is advisable to check it in the Update method to avoid losing information on the input. That’s not difficult, you just need a variable whose value you set in Update and whose value you use in FixedUpdate.

Then you add colliders to your grass and everywhere where the car is not supposed to move.

A third way would be the navmesh. However, I currently do not know if it works with 2D.

I hope this helped. :slight_smile:

Please feel free to ask our helpful community of students for advice over on our Discord chat server.


See also:

I added colliders to the edges of the road to make the car stay there. Is that a bad way of doing it?

Hi Nina,

Thanks much for your replay.
I saw the example for AddRelativeForce, However I still don’t get why just adding colliders to the grass is not enough without the use of relative force.
Another point is that if I want my solution to be more realistic maybe I should allow it to get off the road but to add high friction force that will slow the car when this happen, so how can I do that as well.

Thanks in advance

It is enough if your car is not too fast. Then the physics simulation will still be able to detect collisions and adjust the car’s position. The faster the car becomes, the more likely it is that you get undesired side effects. The best would be to test your idea/approach to see if it works.

In that case, you would implement “my” approach #2 because you need friction, and friction is part of the physics simulation. Then you could create Physic2D materials in the assets folders, increase their respective friction and add the materials to the respective colliders, e.g. your “grass” Physic2D material to the grass collider, your “road” Phyric2D material to the road collider, and so on.

Alternatively, you will have to write your own solution and multiply the moveSpeed by a value depending on the other collider. For instance, if your car collides with the grass, you would multiply by 0.8f, and on the road, you would multiply by 1f, on sand, you would multiply by 0.5f, and so on. Of course, these values are just examples.

At some point, if you start writing your own solutions and ignore the available solutions (e.g. the physics simulation), it might be adviseable to rethink your decision to use Unity. The more solutions work against Unity’s own solutions, the more likely it is that you’ll get undesired side effects and maybe even unsolveable problems. If you write your own solutions anyway, it could make sense to write your own game engine to avoid certain problems.

1 Like

Thanks Nina,
Your answer is very helpful

Hi @Gruvbyggare,

In many cases, there are multiple ways to make something work in Unity. If your approach works, it’s a solution by definition. Unless you see obvious problems such as an increased lagginess, your solution should be fine. :slight_smile:

1 Like

You’re welcome, Lara. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms