- Home /
PlayerController Script Causing Terrible Performance (Profiler says FixedUpdate: LogStringToConsole)
I've been using this player controller script since I first started working on my game, and it's never given me issues until now. The profiler says "PlayerController.FixedUpdate() > LogStringToConsole" is taking up about 96.3% of CPU usage--at least that's what I think it means.
Here is the script:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float maxSpeed = 10f;
public float hold;
public bool facingRight = true;
public bool cantMove;
Animator anim;
public bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float move;
public float jumpForce = 700f;
void Start()
{
anim = GetComponent<Animator>();
hold = maxSpeed;
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool("Ground", grounded);
anim.SetFloat("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
move = Input.GetAxis("Horizontal");
anim.SetFloat("Speed", Mathf.Abs(move));
GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0)
FlipRight();
else if (move < 0)
FlipLeft();
}
void Update()
{
if (grounded && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("Ground", false);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
}
if (cantMove == true)
{
maxSpeed = 0;
}
else
{
maxSpeed = hold;
}
}
public void FlipLeft()
{
Vector3 theScale = transform.localScale;
theScale.x = -Mathf.Abs(theScale.x);
transform.localScale = theScale;
}
public void FlipRight()
{
Vector3 theScale = transform.localScale;
theScale.x = Mathf.Abs(theScale.x);
transform.localScale = theScale;
}
}
I don't have a Debug.Log statement in this script, so I'm unsure of what the profiler means by "LogStringToConsole" in this instance. When I disable the PlayerController script, everything runs very smoothly. However, as I said, I've had this script in my game for a while, and everything has run fine before now.
I had a similar issue, was because lots of warnings were being logged, but I didn't notice since they weren't turned on in the console.
Answer by getyour411 · Sep 08, 2015 at 04:44 AM
Probaby not directly related, but move those GetComponent for Rigidbody calls out of Fixed / Update - define those in Start
See this for additional info on that message (bottom of the thread) http://forum.unity3d.com/threads/profiler-showing-logstringtoconsole-what-is-that.237589/
Answer by wibble82 · Sep 08, 2015 at 06:20 AM
Hey there.
Out of interest, what happens if you right click on the console and choose to 'show editor log'? It could be that one of your function calls is spamming the editor log but you just can't see it. Perhaps a warning about some of those animation parameters you're setting.
While there's clearly an actual issue here, it's worth noting you're doing some expensive stuff in fixed update there. If you haven't done so already, it might be worth looking at your time settings to make sure your fixed update time step is sensible. The default is crazy fast so you might be running that code far more often than you need to.
chris
Answer by sandeepsmartest · Oct 06, 2016 at 04:51 AM
Check your console and try to solve all warnings.Scripts collects more GC if there are any print/debug.log statements or any other warnings.For Eg: if you try to settrigger("Xname") on some animator for which it doesn't have "Xname" parameter(condition) then it definitely throws you warning which results in Garbage Collection. FYI, String operations results in GC. Hope this may help you. Nsks.
Your answer
Follow this Question
Related Questions
Terrible Lag When Starting a Scene Which Eventually Resolves Itself... Graphics Card Issue or Unity? 1 Answer
Editor incredibly slow, weird profiler output. 1 Answer
Problem with profiler spikes caused by Semaphore.WaitForSignal 0 Answers
Performance issue: Camera.Renderer - Drawing - ... - Clear needs 50% CPU ??? 2 Answers
Unity Profiler - Loading.UpdatePreloading - Huge Lag Spikes 0 Answers