I have a project file attached that is problematic. The problem I am experiencing here is an exponential increase in HP damage via a script attached to a replacement object. It seems that the more objects that are instantiated, an inordinate amount of HP is subtracted. The project is fairly simple and I would appreciate any and all feedback.
From what I can tell, you are way out of my league, but I am still happy to try to help - the only problem is that the download link does not work…
I try “download linked file”, which downloads “download.php”, which is shortly replaced by “example_project_158.unitypackage.gz”. Afterwards, I double click the file, and stuffit expander decompresses the .gz file, but then moves on to decompress the .unitypackadge file! This leaves me with a strange folder system.
Do you mind putting the .unitypackadge file in it’s own folder, and then uploading the folder it’s in, not just the plain .unity3packdge file? (please excuse the redundancy)
Not sure why that happened. Are you opening this in Unity Iphone by the way? I placed the project file within another folder per your request, hope that works.
var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);
for (var hit in colliders) {
...
hit.rigidbody.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver);
break;
}}}
The problem is that you get ALL the colliders, and then cycle through each one. Each one of them doing damage to the sun.
I don’t think that’s what you wanted, but I’m unsure of exactly what you do want.
What I did was I added a break at the end of the for loop, which makes the damage only apply once.
I have another question regarding a script to make an object a child of another object. The following script subtracts hitpoints, fades up the material and
lastly, destroys the object and instantiates a dead replacement. The problem is that the object does not become a child of the parent object.
I would like to use these two scripts together to effectively make the object a child of another object.
var deadReplacement : Rigidbody;
var deadReplacementPosition : Vector3;
var deadReplacementRotation : Quaternion;
var dieSound : AudioClip;
var explosion : Transform;
var damagesound : AudioClip;
var initialDelay = 0.0;
var fadeSpeed = 1.0;
var explosionRadius = 5.0;
var explosionPower = 0.0;
var explosionDamage = 25.0;
var explosionTime = 1.0;
var myTransform : Transform;
var hit : RaycastHit;
function Start () {
var explosionPosition = transform.position;
var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);
for (var hit in colliders) {
if (!hit)
continue;
Debug.Log( " hit.collider.tag = " + hit.collider.tag);
if (hit.rigidbody hit.collider.tag == ("Respawn") ) {
Debug.Log(" respawn object hit");
var closestPoint = hit.ClosestPointOnBounds(explosionPosition);
var distance = Vector3.Distance(closestPoint, explosionPosition);
var hitPoints = 1.0;
hitPoints *= explosionDamage;
hit.rigidbody.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver);
break;
}
}
}
renderer.material.color.a = 0.0;
yield new WaitForSeconds(initialDelay);
while (renderer.material.color.a < 1.0) {
color = renderer.material.color;
color.a = 1.0;
renderer.material.color = Color.Lerp(renderer.material.color, color, fadeSpeed * Time.deltaTime);
yield;
}
Destroy(gameObject );
// so this is the object touched in the 2d space
deadReplacementPosition = hit.transform.position;
deadReplacementRotation = hit.transform.rotation;
// Play a dying audio clip
if (dieSound) {
AudioSource.PlayClipAtPoint(dieSound, transform.position);
}
// Replace ourselves with a deadbody
if (deadReplacement) {
var dead : GameObject = Instantiate(deadReplacement, deadReplacementPosition, deadReplacementRotation);
}
This is the code that I’m trying to implement:
if ( myTransform.position.x == hitTransform.position.x) { // matches x position
if ( myTransform.position.y == hitTransform.position.y) { // matches y position
// so this is the object touched in the 2d space
deadReplacementPosition = hit.transform.position;
deadReplacementRotation = hit.transform.rotation;
var parentTransform : Transform = hit.transform.parent.gameObject.transform;
Destroy( hit.collider.gameObject );
// Play a dying audio clip
if (dieSound) {
AudioSource.PlayClipAtPoint(dieSound, transform.position);
}
// Replace ourselves with a deadbody
if (deadReplacement) {
var dead : GameObject = Instantiate(deadReplacement, deadReplacementPosition, deadReplacementRotation);
dead.transform.parent = parentTransform;
}
}
}
}