- Home /
GetMouseButtonDown(0) is laggy
When i start a game and click more times (GetMouseButtonDown(0)) some of the clicks don't work.. i really dont know why? Is it just a lag on my PC or in unity... or it's something with the script
...............................................................................................................................................................
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerScript : MonoBehaviour
{
private bool isInGame = false;
private bool isGameOver = false;
private Rigidbody2D myRB;
Vector2 tempPos;
private bool isRight = false;
private void Start()
{
myRB = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
if (Input.GetMouseButtonDown(0)&& isRight && !isGameOver)
{
tempPos = transform.position;
tempPos.x += 2.0f;
transform.position = tempPos;
print("isRight");
isRight = false;
}
else if (Input.GetMouseButtonDown(0)&& !isRight && !isGameOver)
{
tempPos = transform.position;
tempPos.x += -2.0f;
transform.position = tempPos;
print("!isRight");
isRight = true;
}
if (!isInGame)
return;
}
public void InitGame()
{
isInGame = true;
}
void OnCollisionEnter2D (Collision2D other)
{
if (other.collider.tag == "Enemy")
{
FindObjectOfType<GameScene>().GameOver();
FindObjectOfType<CarsSpawner>().GameOver();
isGameOver = true;
}
}
}
Please format your script correctly. You should also not print anything inside a update function, only for debugging!
Answer by ghostmode · Oct 28, 2017 at 12:13 PM
Try checking for user input in Update()
rather than FixedUpdate()
.
Does that make any difference?
thx mate... it is working well now.. but how ? i thought that FixedUpdate's better what's the difference
Great :)
One isn't better than the other, otherwise there'd be no point in having both.
Update is called every frame (time interval depends on FPS), and is for any non-physics game logic.
FixedUpdate is called at a fixed time interval (not necessarily every frame), and is for working with the physics engine (rigid bodies).
Unity have a short video here: https://unity3d.com/learn/tutorials/topics/scripting/update-and-fixedupdate
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why is the paralaxing shaky? (video examples) 2 Answers
Syncing movement and rotation lag in multiplayer scene 0 Answers
Lag at first second of transform.Translate movement 2 Answers