I’m using AddForce to move my player, and when I run it in the editor, it works great. However, if I build and run it, the forces added seem to be less than when playing in the editor, meaning the player moves a lot slower. It is even worse when running in editor with the animator tab open(much slower than in a build). I have no idea why this is, and cannot seem to figure it out. The script I’m using is below. Thanks for any help!
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour
{
public float moveSpeed = 1f;
public float jumpHeight = 3f;
public float thrusterPower = 1f;
public Transform lineStart, lineEnd;
public bool isGrounded;
public bool thrusterEnabled;
void Update ()
{
if(Input.GetKey(KeyCode.D))
{
rigidbody2D.AddForce(Vector2.right * moveSpeed);
}
if(Input.GetKey(KeyCode.A))
{
rigidbody2D.AddForce(-Vector2.right * moveSpeed);
}
if(Input.GetKey(KeyCode.Space))
{
rigidbody2D.AddForce (Vector2.up * thrusterPower);
thrusterEnabled = true;
}
else
{
thrusterEnabled = false;
}
if(Physics2D.Linecast(lineStart.position, lineEnd.position, 1 << LayerMask.NameToLayer("Floor")))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
if(isGrounded == true)
{
if(Input.GetKeyDown(KeyCode.W))
{
rigidbody2D.AddForce (Vector2.up * jumpHeight * 100);
}
}
}
}
Also, I have a very powerful computer and checked the processes panel and i was only running at 12% memory and 30% CPU.
Thank you for such a quick reply. So how would I go about rewriting the code without using the update function, because GetKeyDown seems to be the only way to get a continuous force acting upon the player?
– EZhurstGetKeyDown is a one-off; GetKey is continuous. You can put GetKey in FixedUpdate, and use GetKeyDown in Update to trigger a bool which is checked in FixedUpdate.
– Eric5h5Yeah, haha I don't know what I was thinking when I commented, I mixed up GetKeyDown with GetKey. Thank you very much.
– EZhurst