1
\$\begingroup\$

I am working on a 2D driving game.

I have a sprite of a car and I set the car movement. Now I need to create a skid mark on the ground. For that I have created an empte game object and added a Trial Renderer component and set the skid mark material to it.

The problem is that when I start moving the car, the skid mark follows the car as if the skid mark is attached to the car.

Is there any simple way to leave a permanent skid mark on the ground in Unity2D?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Create a prefab for the skidmark. When you want to draw a skidmark to your scene, you would instantiate this prefab.

List<GameObject> Skidmarks = new List<GameObject>();

...

void AddSkidMark() {
    //instantiate the prefab
    GameObject skid = GameObject.Instantiate(Resources.Load("SkidMark")) as GameObject;

    //Set some properties
    skid.transform.position = NewPosition;
    skid.transform.rotation = NewRotation;

    //keep reference of all skidmarks
    Skidmarks.Add(skid);
}

If you run into problems with slowdowns because you are instantiating too many skidmarks at once, you are going to want to look into pre-instantiating a bunch of skidmarks and then pooling them for future use.

Utilizing a object pooling is the proper way to tackle this issue.

\$\endgroup\$
4
  • \$\begingroup\$ Can you please help me.New position means the vehicle position? \$\endgroup\$ Oct 6, 2016 at 12:48
  • \$\begingroup\$ In this case it would mean the position and rotation of the tires, the location at which you want to place the skidmarks. \$\endgroup\$
    – jgallant
    Oct 6, 2016 at 14:08
  • \$\begingroup\$ " since its a 2d sprite image how can I do it.I am not getting any idea.I created a empty gameobject and attached the Trail Renderer component and then placed the empty object as the sub of the vehicle.Iam getting the tire mark.but its seems like that the tire mark is following the vehicle when the vehicle is moved \$\endgroup\$ Oct 6, 2016 at 14:15
  • \$\begingroup\$ Sounds like you are attaching the skid mark to the car object instead of world space at the location of the car (and make sure that this action isn't being done on each game tick but rather at whatever event triggers a skid) \$\endgroup\$
    – Neal Davis
    Oct 6, 2016 at 15:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .