Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RichCoggin · Nov 07, 2013 at 10:26 PM · instantiateprefabplayer

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

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tomer-Barkan · Nov 07, 2013 at 10:32 PM 0
Share

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.

avatar image RichCoggin · Nov 07, 2013 at 11:07 PM 0
Share

Thanks, I'll have a look.

avatar image RichCoggin · Nov 07, 2013 at 11:34 PM 0
Share

So I did that and they seem to be at the same position. mmmm

avatar image Tomer-Barkan · Nov 07, 2013 at 11:43 PM 0
Share

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.

avatar image RichCoggin · Nov 08, 2013 at 09:30 AM 0
Share

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...

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges