- Home /
Question by
creativeguy11 · May 08, 2017 at 12:54 PM ·
collidercollision detection
How to make objects collide
I am currently making a game like crossy roads. I have the prefabs and have added colliders to all of them, but when I want to play the game, the player just goes straight through the tree/car. What did I do wrong? Another problem is that my playerHealth script does not work form some reason. Here is my code :
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour
{
public GameObject playerDead;
Vector3 playerPos = new Vector3(0, 0, 0);
float posX;
float posY;
float posZ;
void OnCollisionEnter(Collision collision)
{
posX = transform.position.x;
posY = transform.position.y;
posZ = transform.position.z;
if (collision.gameObject.tag == "car")
{
Destroy(collision.gameObject);
playerPos = new Vector3 (posX, posY, posZ);
GameObject playerDeadIns= Instantiate (playerDead) as GameObject;
playerDeadIns.transform.position = playerPos;
}
}
}
When I come in the way of a car, the car just goes through me and nothing happens to the player. What is wrong?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612121245im_/https://answers.unity.com/themes/thub/images/avi.jpg)