2
\$\begingroup\$

I'm using Unity's standard assets scripts to move my car, but the camera just follows the road.

At certain moments when the car misses or overturns the player can press 'R' to reposition the car in the middle of the road in the position where I miss or turn over.

When the car respawns, it should not collide with other cars in the race, for a few seconds. After those few seconds everything returns to normal.

I thought about using layers, but I did not find how to modify the "Layer Collision Matrix" dynamically from a script. How do I do that?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Why would you change the layer collision matrix, rather than just moving the object to a different layer? \$\endgroup\$
    – DMGregory
    Commented Nov 14, 2017 at 19:43
  • \$\begingroup\$ I did not know that I could change the layer object dynamically. I already found how to do it, thanks. \$\endgroup\$
    – NorbyAriel
    Commented Nov 14, 2017 at 20:01
  • \$\begingroup\$ Feel free to write up your solution as an answer to help future users! \$\endgroup\$
    – DMGregory
    Commented Nov 14, 2017 at 20:01

2 Answers 2

2
\$\begingroup\$

Thanks to DMGregory!!

I Change the layer, I make that layer 10 and 9 not collision, but if the 10 collides with itself:

void Update () {
    delay -= Time.deltaTime;

    if (Input.GetKeyDown(KeyCode.R))
        Respawn();

    if (delay <= 0)
        gameObject.layer = 10;
}

private void Respawn()
{
    transform.position = new Vector3(OriginalPosition.x, OriginalPosition.y, transform.position.z-2);
    transform.rotation = OriginalRotation;
    if (rbCar != null)
    {
        rbCar.velocity = Vector3.zero;
        rbCar.angularVelocity = Vector3.zero;
    }
    delay = 5;
    gameObject.layer = 9;
}
\$\endgroup\$
0
\$\begingroup\$

While the OP has already found a solution I figured I would give my input.

Yes, changing the game objects layer is one way of stopping the car from colliding with other objects, however, another way you could do this is by setting the Rigidbody.isKinematic property to false for the duration you wish the object to not detect & resolve collisions.

Of course this will stop the car from reacting to any physics, which in this case, the OP would more than likely want the user to be able to continue driving the car when it is re-spawned, but I just figured it would be worth the mention.

\$\endgroup\$

You must log in to answer this question.

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