I am trying to make an endless runner where there are obstacles varying in height. To overcome these, every time the player taps the screen, I want a prefab to pop under the player and then move along with the player, that is until there appears an obstacle, when there appears an obstacle I want the prefab to collide with the particle and remain there while the player on top of it is able to move forward. I want the player to be able to Instantiate the prefab as many times as they want, but each time the prefab must spawn directly under the player and move along with it. The player continuously moves along the Z axis and to achieve this I have used “transform.position”.
However I am a beginner in Unity and am genuinely confused on how to go about this, any pointers or solutions would help a lot, because I don’t know how to go about spawning prefabs directly under player and moving them along with the player.
TLDR: How do I spawn a prefab under the player so that the player gets lifted up due to the object spawning beneath it?
Instantiate the object at the transform location of the player. To make the object follow the player you could put a script on the object that puts the transform of the object at the location of the player every frame, or you could make the object a child of the player object and it would be carried along until you unparent it when it gets hit but keep it in its current position, or you could give it a script that makes it move at the same speed as the player until it gets hit…
Hello there, I have implemented instantiate using the following code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawn : MonoBehaviour
{
public Transform SpawnPoint;
public GameObject Prefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if ((Input.touchCount > 0) || (Input.GetMouseButtonDown(0)))
{
Instantiate(Prefab, SpawnPoint.position, SpawnPoint.rotation);
}
}
}
I have placed an empty game object underneath the Player and now whenever I touch the screen or click, the prefab appears right below it. Causing its path to be disrupted, due to that, it gets left behind. I will try making it bump and will also try to add a script for parenting and unparenting the object and let you know if there are any issues
EDIT: @QuinnWinters As you said, I decided to make the character jump whenever the spawn is triggered, for that I have used: GetComponent<Rigidbody>().AddRelativeForce(Vector3.up * jumpForce);
But whenever there is a repeated touch the Player goes higher than normal since the force gets added up twice, I do not want it to do so. How would that be possible? Examples with code would be very appreciated :^)