Hello All-
I am new to Unity scripting and am working on a learning project.
In my scene, I have an object that when it gets hit (using the raycasting system) instantiates 2 new prefabs. With the current script, the newly instatiated objects are smaller than the original object hit.
Here's the problem: when I hit one of the new, smaller instatiated objects, it produces 2 new objects of the same size, not smaller than the object just hit. (The general idea being that whatever object you hit produces 2 new objects of a smaller size - so in theory you can keep making smaller and smaller objects)
I'm guessing that I'm not getting the scale of the object hit and instead keep getting the scale of the original prefab. Help please?
Thank you!
Here's the code:
if (Physics.Raycast ( transform.position, gunDirection, gunHit, gunRange) )
{
print ("Hit something");
//Add an explosion
explosionClone = Instantiate (explosion, gunHit.point, transform.rotation);
// Did we hit something with a rigidbody?
if (gunHit.rigidbody)
{
print ("I hit a rigidbody");
// Apply force to the object
gunHit.rigidbody.AddForceAtPosition (gunForce * gunDirection, gunHit.point);
for (var i=0; i<2; i++) {
var hitScale = transform.localScale;
print (hitScale);
// Make new barrel
respawnClone = Instantiate (respawnPrefab, gunHit.point, transform.rotation);
// Rescale the new barrel
var smaller = hitScale * 0.9;
print (smaller);
respawnClone.transform.localScale = smaller;
// Add to counter
barrelCounter.barrelsOn ++;
}
}
}