- Home /
Player sometimes lags and jumps very high when instantiating prefab
Hello, I am trying to spawn prefabs beneath the player using the following 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); //0.2f bc if you simply write 0.2 it will give error
//Debug.Log("Wait is OVER!");
//Instantiate(Prefab, SpawnPoint.position, SpawnPoint.rotation)
Instantiate(Prefab, SpawnPoint.position, SpawnPoint.rotation).transform.parent = GameObject.Find("Player").transform;
}https://answers.unity.com/questions/ask.html#
// 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!
Answer by Mashimaro7 · Aug 10, 2020 at 01:00 PM
The issue is probably that your jump is in Update(). I'm not 100% sure why it's causing lag, might be that your prefabs are too complex? Just did some basic testing, and I think that timeCounter is a less than optimal way to prevent jumping when in air. What I do is have a grounded bool, have an OnCollision and OnCollisionExit method, set it to true on the collision, false on the exit. Then set the jump to only work if(grounded). Another thing is, the
GetComponent<Rigidbody>().AddRelativeForce(Vector3.up * jumpForce);
and any other movement codes, should be moved to a FixedUpdate() rather than an Update()
Also, this timeCounter, is that the same as the wait time for the coroutine? You can declare it in the coroutine like
IEnumerator waitSpawn(int delay)
And then in the "StartCoroutine" part, put something like this StartCoroutine(waitSpawn(timeCounter));
Sorry if I'm not understanding fully, but what are you trying to do with the spawning objects? Is it some sort of effect? I feel like they should probably be spawned in a separate function, but I could be wrong.
Sorry, I meant to say the issue of your character jumping super high when it lags, is most likely caused by the jump being in the Update() method
Thank you so much! I will move them into fixed and late update and let you know of the progress.
Hi, my prefabs are pretty simple, a cube with a color material on it, but I will try changing them into fixed and late update as you said, will keep you updated.
You don't need a character controller, just check for collisions with the box collider(or whatever collider you have attached)