Hello,
I have a scene where there are many objects of different size. The plays runs around bumping into them. I need a different reaction if the objects are larger or smaller. Calculating their size and player’s size is no problem, I tested and saw they all have a different value. but when I get to the collision test, I get stuck on the same piece of data from some object, and it is not re-checking the data for all objects.
This is my collision code:
function OnCollisionEnter(hit : Collision)
{
if(hit.gameObject.tag == "object")
{
var colliderSize = hit.gameObject.GetComponent(ObjectControl).objectSize;
if (colliderSize < playerSize)
{
Destroy(hit.gameObject); // if object is bigger, change here to push the player or some type of feedback + sound
playerSize +=1;
print("Yummy! I ate a " + colliderSize+ " and my size is now: " + playerSize);
}
else
{
Destroy(hit.gameObject); // if object is bigger, change here to push the player or some type of feedback + sound
playerSize -=1;
print("I've been hit! " + colliderSize + " by a " + colliderSize);
}
}
}
This is the bit of code from each object. They change colour depending if they are smaller or larger than the player’s current size:
static var objectSize = 1.0;
var swapMat : Material;
var originalMat : Material;
function Update ()
{
var Player = gameObject.Find("player");
var PlayerSize = Player.GetComponent(PlayerInteractions).playerSize;
objectSize = (transform.localScale.x);
if (objectSize > PlayerSize) //object is larger than player
{
// paint object red
renderer.material = swapMat;
}
if (PlayerSize > objectSize ) //if smaller than player
{
//paint object green
renderer.material = originalMat;
}
}