- Home /
Instantiating at wrong values
I have a zombie enemy, when the enemy dies, I am trying to instantiate a ragdoll version over it. The instantiating is working out fine, it applies to correct transform values, however these values applied to the ragdoll mesh are entirely outside of the map. Example being the zombie enemy has a y value of ~161 (and is on the map), but this same value on the ragdol mesh is way below the map (ragdoll needs a value in the 800s to be close to where the map is). Anyone have an idea what is happening here? Thanks
Edit: Sorry about that! The instantiating code is as follows:
currentPosition = transform.TransformPoint(Vector3.zero);
GameObject Doll;
Doll = Instantiate(Rag, currentPosition, transform.rotation) as GameObject;
Rag is the Ragdoll model declared at the beginning as a GameObject currentPosition is declared as a vector3 at the beginning. currentPosition is created as transform.TransformPoint(Vector3.zero);
I originally had transform.position in instantiate but when it didn't work, I figured I'd get the zombie's current position and put that in instead. Please let me know if there is any other info which would help to solve this one. Thanks again!
Full code:
public int ZombieTotalHealth = 5; public int ZombieLives = 1; private int ZombieHealth; public GameState gameController;
public GameObject Rag;
private Vector3 currentPosition;
// Use this for initialization
void Start () {
ZombieHealth = ZombieTotalHealth;
}
// Update is called once per frame
void Update () {
currentPosition = transform.TransformPoint(Vector3.zero);
if(ZombieHealth <= 0)
{
Die();
}
}
public void ApplyDamage(int damage){
ZombieHealth -= damage;
}
public void Die (){
GameObject Doll;
Doll = Instantiate(Rag, currentPosition, transform.rotation) as GameObject;
Destroy(gameObject);
}
}
Forgot to mention that this script is attached to the zombie, and the ragdoll is just the ragdoll mesh with no current script on it, it should just be instantiating at the point where the zombie was.
I tried using transform.position too, but that gave the same issue. The ragdoll mesh inherits the correct values from the zombie mesh, but for some reason the ragdoll coordinates with those values are outside the map. As said up above, the zombies have a y value of ~161, but the ragdoll needs a value in the 800s to be at the same y position as the zombies. - This is after the ragdoll is instantiated, the master ragdoll is at the appropriate location if given a y value of ~161.
Please share some code :D
We can understand your issue much better if you share code.
As @dorpeleg says, we need to see some code. In particular we need anything that assigns or modified currentPosition.
You need the position of the zombie, but, unless this script is attached to the zombie, there is nothing here that gives that position. If this script is attached to the zombie, then transform.position is the correct value. But you say "get the zombie's position," so I'm not sure how you have your object/scripts structured.
Answer by Auggie · Mar 19, 2013 at 12:32 AM
I don't know what effect you are trying to achieve here but if you just want it to be instantiated where your zombie dies, then i see no reason why not to use this code:
public void Die (){
GameObject Doll = Instantiate(Rag, transform.position, transform.rotation) as GameObject;
Destroy(gameObject);
}
i don't think you really need to use transform.TransformPoint because transform.position is set to world space already. while transform.TransformPoint, if i'm not mistaken, uses the value of your localPosition to worldPosition. which would explain why your position vector is a bit off
Answer by Maielstrom · Mar 19, 2013 at 04:20 PM
I found out what the problem was! I had the zombie ragdoll model floating in space, causing it to fall straight down into an endless void. I created a "box" to hold it in using planes so it remains at the spot I put it, and now it spawns in the correct place where it's zombie host dies
That should not happen.
Unless your ground has no colliders.
The instantiated ragdolls are spawned inside the map. The ragdoll which was falling was the master ragdoll which is cloned at the time of instantiation. I put the master one outside of the map (as this is how I was taught to deal with cloning/instantiating), and thus it just fell at the start as there was nothing underneath it
make your ragdoll into a prefab. that is: drag it from the heirarchy view panel and put it in a folder on the project view panel. and make that prefab as the gameobject to be instantiated. then delete the ragdoll on heirarchy
Wow, never knew that one! Thanks, definitely makes things a lot easier