1
\$\begingroup\$

I am working on a 2D driving game.I have a sprite of a car.Set the car movement.Now I need to create a skid mark on the ground for that I have created a empte game object and added 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 that a skid mark is attached to the car.

Is there any simple way to leave a permanent skid mark on the ground in unity2d. Can anybody please help me

\$\endgroup\$
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 '16 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 '16 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 '16 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 '16 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.