GameObjects move slower at 9 FPS or less.
Hi. I'm getting started in Unity and currently I'm working in a TowerDefence 2D game. I am having an issue with the GameObjects and their movements. Some gameObjects use rigidbodies to move and other use "transform.Translate", but both get the same problem. I know that the frames per second affect the Update() method, so if there are less FPS or more, the velocity of the GameObjects that use that method to move can be different. I have looked in other Unity answer pages for this and I changed the method to FixedUpdate(). Now here is the problem:
,
Now the velocity of the GameObject is constant, no matter what FPS there are, but if there are less than 10. I've tried with 400 FPS, 300FPS, 120FPS, 60FPS, 30FPS, 10FPS. In all of these the speed of the GameObjects is the same and finishes moving at the same time, but when I change the FPS limit to lower than 10, the speed starts to change. At 5 FPS it can be different by some seconds to finish the movement stablished by SCRIPT. Now the issue that was solved was showing in this range, 1 to 9 fps. The more Fps in these, the faster, but the GameObjects always move slower than at 10 or more FPS (that it is constant).
,
I leave here one SCPIPT that I use. It isn't the only one, but all of the others are the same, but with other functions of hurting and dealing damage to other objects.
,
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Proyectil : MonoBehaviour {
GameObject[] enemyes;
List<GameObject> collisions = new List<GameObject>();
public Sprite sprite1;
public Sprite sprite2;
float tiempo = 0f;
[HideInInspector]
public bool aliado;
[HideInInspector]
public float velocity;
[HideInInspector]
public bool area;
[HideInInspector]
public int dañar;
[HideInInspector]
public float yPosition;
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
tiempo = 0f;
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate()
{
if (aliado == true)
{
rb.velocity = Vector2.right * velocity;
}
else
{
rb.velocity = Vector2.left * velocity;
}
gameObject.transform.position = new Vector3(gameObject.transform.position.x, yPosition, gameObject.transform.position.z);
}
void Update()
{
tiempo += Time.deltaTime;
if (tiempo > 0.5f)
{
gameObject.GetComponent<SpriteRenderer>().sprite = sprite2;
}
else
{
gameObject.GetComponent<SpriteRenderer>().sprite = sprite1;
}
if (tiempo > 1f)
{
tiempo = 0f;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (aliado == true)
{
if (collision.transform.tag == "Enemigo")
{
collisions.Add(collision.gameObject);
enemyes = collisions.ToArray();
if (area == false)
{
enemyes[0].GetComponent<Enemy>().hurted2(dañar);
}
else
{
foreach(GameObject value in enemyes)
{
value.GetComponent<Enemy>().hurted2(dañar);
}
}
//ATAQUE
Destroy(gameObject);
}
}
else
{
if (collision.transform.tag == "Alimento")
{
if (collision.GetComponent<Alimento>().Inicio == true)
{
collisions.Add(collision.gameObject);
enemyes = collisions.ToArray();
enemyes[0].GetComponent<Alimento>().hurted(dañar);
Destroy(gameObject);
}
}
if (collision.transform.tag == "PLAYER")
{
collisions.Add(collision.gameObject);
enemyes = collisions.ToArray();
enemyes[0].GetComponent<jugador>().hurted(dañar);
Destroy(gameObject);
}
}
if (collision.transform.tag == "ParedParProy")
{
Destroy(gameObject);
}
}
Summary:
I put in the movement of all GAMEOBJECTS in FixedUpdate() method and their speed is always constant in all FPS but at 9 FPS or less. If FPS are less than 10, their speed of movement change and they become slower. If someone know something about this, please tell me. Thanks for the help, hope I explained well and that the script is enought.
Your answer
Follow this Question
Related Questions
Problems with Movement on rigged 2d animation., 0 Answers
I want to rotate the 2D missile with touch controls 1 Answer
Having multiple UNET errors that I believe are caused by unity itself. Am I doing something wrong? 1 Answer
Limiting backwards speed ,Limiting backwards speed 0 Answers
[UNET] Having problems with clients not being able to shoot after they reload 0 Answers