- Home /
4.6.0 to 4.6.1 makes player lag, bug?
Hi, so I was working on a prototype for my game and was using the 4.6.0 version. When I updated to the current version of 4.6.1, my character seemed to randomly start lagging and I have no idea why. I used the same code for both of them and made no modifications. To make my player move I used rigidbody2D.AddForce. I've tried uninstalling and re-installing unity multiple times and keep having the same problem. A weird though, the background doesn't lag, just the player does. I made a little demonstration and uploaded it to YouTube:
Could it be a problem with one of my settings? I know that on the 4.6.1 Release Notes it mentions something about Physics2D and such. I thought this was the reason but it doesn't mention anything about the AddForce function.
Here's the code I used. The movement code is in the update function and I don't see any problems with it:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float speed = 4f;
public bool surfaceTouch = false;
public bool SurfaceFall = false;
public ParticleSystem Splash;
public ParticleSystem Sand;
public ParticleSystem AirBubbles;
private Animator anim;
// Use this for initialization
void Start () {
rigidbody2D.gravityScale = 0;
Sand.emissionRate = 0;
Splash.emissionRate = 0;
AirBubbles.emissionRate = 0;
anim = gameObject.GetComponent<Animator> ();
StartCoroutine (bubbleAir ());
}
// Update is called once per frame
void Update () {
if (Time.timeScale == 1) {
//Face Mouse
Vector3 mousePos = Input.mousePosition;
mousePos.z = 0f;
Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
var speedTurn = 500f;
float angle = Mathf.Atan2 (mousePos.y, mousePos.x) * Mathf.Rad2Deg;
Quaternion qTo = Quaternion.Euler (new Vector3 (0, 0, angle));
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, speedTurn * Time.deltaTime);
//Movement
var pos = Input.mousePosition;
pos = Camera.main.ScreenToWorldPoint (pos);
var dir = pos - transform.position;
rigidbody2D.AddForce (dir * speed);
}
//Animation
anim.SetInteger ("state", 0);
var absVel = Mathf.Abs (rigidbody2D.velocity.magnitude);
if (absVel < 1f) {
anim.speed = 0.1f;
} else if (absVel > 1f && absVel < 3f) {
anim.speed = 0.3f;
} else if (absVel > 3f && absVel < 5f) {
anim.speed = 0.7f;
} else if (absVel > 5f) {
anim.speed = 1f;
}
}
void OnTriggerEnter2D(Collider2D col){
if (col.gameObject.tag == "Surface") {
//Splash
if(surfaceTouch == false){
surfaceTouch = true;
Splash.gravityModifier = -1;
Splash.emissionRate = 100;
StartCoroutine(splash());
}
}
//Sand
if (col.gameObject.tag == "Floor") {
var absVelY = Mathf.Abs (rigidbody2D.velocity.magnitude);
if (absVelY > 1) {
Sand.emissionRate = 30f;
StartCoroutine (stopSand ());
} else if (absVelY < 1) {
Sand.emissionRate = 15f;
StartCoroutine (stopSand ());
}
}
}
void OnCollisionEnter2D(Collision2D col) {
if (col.gameObject.tag == "Surface") {
var absVelY = Mathf.Abs (rigidbody2D.velocity.y);
if(absVelY < 0.5){
rigidbody2D.gravityScale = 0.6f;
SurfaceFall = true;
StartCoroutine( SurfaceDown() );
} else if (absVelY < 1 && absVelY > 0.5){
rigidbody2D.gravityScale = 1f;
SurfaceFall = true;
StartCoroutine( SurfaceDown() );
} else if (absVelY > 1){
rigidbody2D.gravityScale = 1.5f;
SurfaceFall = true;
StartCoroutine( SurfaceDown() );
}
}
}
//spash
IEnumerator splash(){
yield return new WaitForSeconds (0.3f);
Splash.gravityModifier = 1f;
yield return new WaitForSeconds (0.4f);
Splash.emissionRate = 0;
surfaceTouch = false;
}
//Sand
IEnumerator stopSand (){
yield return new WaitForSeconds (0.5f);
Sand.emissionRate = 0;
}
//Surface
IEnumerator SurfaceDown(){
if (SurfaceFall == true) {
yield return new WaitForSeconds(0.2f);
rigidbody2D.gravityScale = 0;
SurfaceFall = false;
}
}
//Air bubbles
IEnumerator bubbleAir(){
var bubbles = 5;
while(bubbles > 0){
yield return new WaitForSeconds (10);
AirBubbles.emissionRate = 7f;
yield return new WaitForSeconds(2);
AirBubbles.emissionRate = 0f;
}
}
}
If you have any ideas what this could be, I'm all ears! Thanks!
Answer by jpthek9 · Jan 08, 2015 at 04:50 PM
My guess is right now your Rigidbody (character) is calculating its position inconsistently. Either that, or you're not using Time.deltaTime to make the movements frame-independent.
Do physics in FixedUpdate and if you already are, set the Fixed Timestep in your Time Manager to be lower (http://docs.unity3d.com/Manual/class-TimeManager.html). You can also set Rigidbody.interpolate so updates automatically interpolate your Rigidbody's position.
Also, make sure you multiply calculations by Time.deltaTime so a faster or slower frame rate doesn't affect you. http://docs.unity3d.com/ScriptReference/Time-deltaTime.html
Your answer
Follow this Question
Related Questions
Release Unity Player as open-source? 1 Answer
,Unity 2020 Player mesh bug 0 Answers
Network Game: How to solve : problem Lag or Refresh frame? 1 Answer
Player not able to handle edges of blocks? 0 Answers
my lives system is buggy 0 Answers