- Home /
Player clone not instantiating at the same spot
Hi there,
I have a player character that moves forward of it's accord with a jump function. When it get's killed - by hitting an object a prefab (dead character prefab) is instantiated in its place. Trouble is, is that when it's instantiated it doesn't appear at the exact same spot. It seems to be - 4 below every time or when the player character is in the air.
Here's the code for the Player character for running:
var speed: float = 4.0; // move speed
var jumpSpeed: float = 8.0; // initial jump speed
var gravity: float = 9.8;
private var cc: CharacterController;
private var vSpeed: float;
function Update(){
var moveDir = transform.forward * speed; // calculate the horizontal speed
if (!cc) cc = GetComponent(CharacterController); // get the CharacterController
if (cc.isGrounded)
{ // when grounded...
vSpeed = 0.0; // vSpeed is zero...
if (Input.GetButton ("Jump")) {
vSpeed = jumpSpeed;
}
}
vSpeed -= gravity*Time.deltaTime; // apply a physically correct gravity
moveDir.y = vSpeed; // add the vertical speed
cc.Move(moveDir*Time.deltaTime); // and finally move the character
}
And here's the code for the instantiated of the dead character:
function Dead(){
///Replace Player with Dead Player object
var PlayerDead = Instantiate(PlayerDead, transform.position, transform.rotation);
///remove Player object
Destroy(Player);
}
Any ideas would be muchly appreciated!
Rich
Your instantiation code should work just fine to create the dead player in the same location of the player (assu$$anonymous$$g the script is attached to the player).
Could be that the "Center" of each prefab is different, hence even though they are both located in the same position, they appear to be in different position.
You can easily check it - drag both prefabs to the scene. Set the position of each of them to 0,0,0. Then look and see if they appear to be in the same position.
So I did that and they seem to be at the same position. mmmm
Hmmm... so we'll need more info. Is it a single object or object hierarchy? $$anonymous$$aybe the script is not attached to the player, but to some child/parent of the player character? Try debug logging - the player's transform.position before instantiating, the dead player's transform.position after instantiating it, the dead player's transform.position in it's Start() method...
Also, see that there's no script attached to the dead player that might modify his location. $$anonymous$$aybe even try a new scene, with only the player (exact same prefab that you use in the game), and in the player's Start()
method try instantiating the dead player, see what happens... then slowly add more functionality from your game until it breaks, that way you will know what broke it.
Thanks, I'll go through this. The player is an object hierarchy, with only a graphic/object under the main player. This is set to zeros. He only script attached to the dead player is a kill script.
I'll try testing.
Thanks for your comments so far...
Answer by HSMInteractive · Nov 07, 2013 at 11:18 PM
The center of each prefab is probably different. Make sure they have the same center location.
Your answer
Follow this Question
Related Questions
What to set to make a Instantiated prefab a collider to trigger and event? 1 Answer
Instantiate a prefab and follow player position in runtime? 0 Answers
Problem when assigning a prefab 1 Answer
Instantiating random prefabs when the player passes through a trigger 1 Answer
Multiplayer prefab player fall through floor on instantiate 0 Answers