Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by RelapseHD · Apr 18, 2017 at 11:58 PM · instantiaterigidbody2dgravitytransform.positionspawning

Spawn Object under Moving Player

So my main player has a rigidbody2d with gravity meaning it is falling, but i want to spawn an object under the player and when i try to do so with the code below it only spawns the object in the first position and it doesn't update when the player position changes because it is falling.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class BirdSpawn : MonoBehaviour {

 public GameObject bird;
 public GameObject player;
 public float delayTimer = 0.5f;
 private Vector3 offset = new Vector3 (-5f, -2f, 0);
 float timer;


 void Start () 
 {
     timer = delayTimer;
 }


 void Update () {

     timer -= Time.deltaTime;
     if (timer <= 0) 
     {
         Vector3 birdpos = transform.position;
         transform.position = player.transform.position + offset;
         Instantiate (bird, birdpos, transform.rotation);
         timer = delayTimer;
     }
 }

}

Comment
Add comment · Show 3
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 Matthewj866 · Apr 19, 2017 at 12:18 AM 0
Share

Hi there! can you clarify what you mean by "doesn't update"? I'm not entirely sure what you want to do.

avatar image RelapseHD Matthewj866 · Apr 19, 2017 at 08:22 PM 0
Share

the spawn area for the object is the players coordinates plus an offset but the coordinates doesn't change when the player is falling down so basically the transform.position doesn't update when the coordinates of the player changes.

avatar image Matthewj866 RelapseHD · Apr 19, 2017 at 09:09 PM 0
Share

If they aren't updating at all, with the snippet you've given then something must be wrong with the if condition, which I find hard to believe because that's perfectly fine. Does anything else affect the position?

Also, birdpos will always have the old position of the spawner, so it will always spawn in the spawner's previous location.

0 Replies

· Add your reply
  • Sort: 

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

113 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make instantiated rigidbodies continue moving the in the same direction as destroyed object? 1 Answer

instantiate keeps repeating itself 0 Answers

How do make a sprite of a drinking glass have a solid bottom and side with gravity 1 Answer

Position Ball 2 Answers

How do I modify a game object component from a script attached to another game object? 1 Answer


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