Reputation: 167
So I just finished writing my movement script and my game seems like it has a low framerate. I booted up fraps and found that my game is running at 60FPS. What could be the issue? Also this is a top down RPG style game by the way. Here's my movement script if that helps:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
Vector2 _playerPosition;
public GameObject Player;
// Use this for initialization
void Start () {
_playerPosition = Vector2.zero;
}
// Update is called once per frame
public float speed = 3f;
void Update()
{
if (Input.GetKey(KeyCode.W))
{
transform.position += Vector3.up * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
transform.position += Vector3.down * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.D))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
}
}
Upvotes: 1
Views: 100
Reputation: 896
Watching YouTube tutorials can be real helpful in learning new things about Unity. look here at 4 min and you will see code that I would try for your transform like this:
if (Input.GetKey(KeyCode.D)){
transform.Translate(speed * Time.deltaTime,0f,0f); //x,y,z
}
The suggestion I had in the question's comment, I would put your if statements inside a method outside the update and call the method say every second like so, which Unity has a good community of question/answers as well
InvokeRepeating("MyMethod", 1f, 1f); //I believe this is every second
I would also make a suggest change to your code that would reduce the lines and allow for movement keys of left,right,up,down as well as A,D,W,S and us of joystick movements.
void Update(){
transform.Translate(speed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f,
speed * Input.GetAxis("Vertical") * Time.deltaTime)
}
Upvotes: 1