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.