Well what I’m trying to do is have cars travelling on the road. The cars have a trigger at the front and a hitbox at the back of the cars. What I want to happen is when the trigger hits the hitbox it compares the Variables for speed of the two cars in the collision, i.e. the make the car at the back slow down or equal the speed of the car in the front. However, what I am having trouble with is referencing the speed of both cars and comparing them. This is due to them both being prefabs using the same script. Which I’m learning means that I’m just referencing the same script and not the actual values of the individual car. But how could I get round this? Is it possible to have collisions occurring between objects using the same script?
private void OnTriggerStay2D(Collider2D collision)
{
Debug.Log("TriggerStay");
GameObject collidedParent = collision.gameObject.transform.parent.gameObject; // get parent transform and then its gameobject
Traffic Col_speed = collidedParent.GetComponent<Traffic>();
if (speed > Col_speed.speed)
{
Accelerate = false;
ApplyBreak = true;
}
else if (speed < Col_speed.speed)
{
Accelerate = false;
ApplyBreak = false;
speed = Col_speed.speed;
}
if (transform.position.y > collision.transform.position.y)
{
turn = turn + 1;
Debug.Log(this.gameObject.name + "someMessage");
}
if (transform.position.y < collision.transform.position.y)
{
turn = turn - 1;
Col_speed.turn = 0;
Debug.Log(this.gameObject.name + "someMessage");
}
}
private void OnTriggerExit2D(Collider2D collision)
{
turn = 0;
Accelerate = true;
ApplyBreak = false;
}
}