I am trying to instantiate a pickup prefab within a room when an enemy is killed. I have tried to set it to where the prefab is instantiated at the position of the enemy. Simple, right?..
…not exactly. I tried the easy, obvious, really-should-be-correct position for the instantiated object (transform.position), but that nearly always had the item instantiate out of the room. (Just to let you know, the object which instantiates the prefab is within the room, is a child of the enemy, and is exactly where I want). Then I thought that I would try to use Mathf.Clamp() to limit where the prefab would instantiate. That worked partly, with the x and z values within proper range, but the y values were still off (it either wasn’t in the room or out of reach about two-thirds of the time). I thought that I would let the x and z values be, and set the y value to a constant float. This is functional; the prefab doesn’t usually instantiate with the proper y value, but is within reach two-thirds of the time, with the other one-third being under the room.
Does anyone have an idea of how to fix this perplexing problem? The code that I am using to instantiate the prefab (which is of a key, btw) is shown below.
private var key : GameObject;
var xMax : float;
var xMin : float;
var yPosit : float;
var zMax : float;
var zMin : float;
function Start () {
key = transform.gameObject.parent.MarshmellowManDamage.key;
}
function OnDestroy () {
var obj = Instantiate(key,Vector3(Mathf.Clamp(transform.position.x,xMin,xMax),Mathf.Clamp(transform.position.y,yPosit,yPosit),Mathf.Clamp(transform.position.z,zMin,zMax)),Quaternion.identity);
obj.transform.position.y = yPosit;
}
================================================================
Edit : I have changed the scaling of the prefab and the enemy, but the enemy now either flies around the room, or just drops and the keys are still a problem.