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;
}
}
}
Hi there! can you clarify what you mean by "doesn't update"? I'm not entirely sure what you want to do.
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.
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.
Your answer
Follow this Question
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