Player sometimes lags/glitches and jumps very high when instantiating prefab beneath it

Hello,
I am trying to spawn prefabs beneath the player using the following script(more explanation below the script):

using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.EventSystems;
  
    public class Spawn : MonoBehaviour
    {
        public float jumpForce;
        public Transform SpawnPoint;
        public GameObject Prefab;
  
        public Transform player;
  
        float timeCounter;
  
        // Start is called before the first frame update
        void Start()
        {
  
        }
  
        IEnumerator waitSpawn()
        {
            yield return new WaitForSeconds(0.2921f);
            Instantiate(Prefab, SpawnPoint.position, SpawnPoint.rotation).transform.parent = GameObject.Find("Player").transform;
        }  
        // Update is called once per frame
        void Update()
        {
            if (player.transform.position.y < 12f)
            {
                if (player.transform.position.y > -4f)
                {
  
                    timeCounter += -1 * Time.deltaTime; //to set the time before player can touch screen again
  
                    if ((Input.touchCount > 0))
                    {
                        foreach (Touch touch in Input.touches) //so that it does not continue going up if screen is continuously touched
                        {
                            if (touch.phase == TouchPhase.Began && !(EventSystem.current.IsPointerOverGameObject(touch.fingerId)))
                            {
                                if (timeCounter < 0)
                                {
                                    timeCounter = 0.29f; // Or whatever delay you want
                                    GetComponent<Rigidbody>().AddRelativeForce(Vector3.up * jumpForce);
                                    StartCoroutine(waitSpawn());  
                                }
                            }
                        }
                    }
                }
            }
  
            if (Input.GetMouseButtonDown(0))
            {
                if (timeCounter < 0)
                {
                    timeCounter = 0.29f; // Or whatever delay you want
                    GetComponent<Rigidbody>().AddRelativeForce(Vector3.up * jumpForce);
                    StartCoroutine(waitSpawn());
                }
            }
    }

I make the player jump using add force whenever the screen is tapped or the mouse is clicked so that the object does not spawn right under the player, causing it to glitch and fly really high, I also tried to add a pause before the player can spawn objects again, but this still happens sometimes.
I would appreciate if someone told me a way to fix this, code examples are greatly appreciated, I am a beginner so this way I can learn some more too. Thank You!

When you Instantiate something that is already overlapping in the physics system, you essentially surprise the physics system with a sudden massive collision problem it has to solve and it can only do so with forces.

The collision rules say the player can’t be inside the object, so the physics system begins computing a massive force to squirt the player out.

Instead, make sure the player is not overlapping with the instantiated object’s position (including collider) before you spawn it. Or otherwise deconflict it some way. OR… you can spawn the object much further below the player, and then use the Rigidbody .MovePosition() call to move it into place smoothly (using a coroutine or in Update()) under the player, giving the player time to get out of the way.

Either way , don’t startle the Physics system. Think what would happen if a car suddenly spawned right where you were standing… you’d have to shoot out to one side pretty quickly.

Thanks for replying, in order to not startle the physics system, I have put the spawn position below the player(not inside of it), and then I made the spawn position a child of maiin player in order to move the spawn position along with it. I will try using RigidBody.MovePosition() and keep you updated

1 Like