Search Unity

How to: instantiate prefab under player position

Discussion in 'Scripting' started by phantom_x404, Aug 1, 2020.

  1. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    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?
     
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    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..
    Code (CSharp):
    1. Instantiate (myObject, playerTransform.position, Quaternion.identity);
    https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

    To make it bump up you could apply an upward force or movement to the player at the same time you instantiate.
     
  3. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    Hello there, I have implemented instantiate using the following code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spawn : MonoBehaviour
    6. {
    7.     public Transform SpawnPoint;
    8.     public GameObject Prefab;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.  
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if ((Input.touchCount > 0) || (Input.GetMouseButtonDown(0)))
    20.         {
    21.             Instantiate(Prefab, SpawnPoint.position, SpawnPoint.rotation);
    22.         }
    23.     }
    24. }
    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:
    Code (CSharp):
    1. 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 :^)
     
    Last edited: Aug 1, 2020