Play mode working differently on different computers
I have been having an issue with the editor (Unity 2019.3.0f6) where the player character is moving significantly slower than expected in Play mode. However, on my laptop, the character moves as expected in Play mode. Both computers have the player character behaving as expected in Build mode and the only discrepancies appear in Play mode. The following is the relevant portion of my Update function:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Fungus;
using System.Threading;
public class PlayerController : MonoBehaviour
{
Rigidbody2D rigidbody2D;
public float moveSpeed = 6;
public static Vector2 PlayerCoordinates { get; private set; }
Animator animator;
public Vector2 lookDirection = new Vector2(0, 0);
public float StartX;
public float StartY;
public Vector2 StartingPosition;
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector2 position = rigidbody2D.position;
Vector2 move = new Vector2(horizontal, vertical);
if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
{
lookDirection.Set(move.x, move.y);
lookDirection.Normalize();
}
animator.SetFloat("Look X", lookDirection.x);
animator.SetFloat("Look Y", lookDirection.y);
animator.SetFloat("Speed", move.magnitude);
position.x += (moveSpeed * horizontal * Time.deltaTime);
position.y += (moveSpeed * vertical * Time.deltaTime);
PlayerCoordinates = position;
rigidbody2D.MovePosition(position);
}
}
I have checked and set Time.timeScale to 1 and the issue hasn't been resolved. In addition, coroutines that also use Time.deltaTime are unaffected and behave as expected on both computers and in both Play and Build. It seems only Update is affected.
EDIT: I've resolved the issue. It was caused by floating point errors in Time.deltaTime and was fixed by capping the framerate in the Editor.
#if UNITY_EDITOR
Application.targetFrameRate = 60;
#endif