Script is Seemingly too slow for Mouse Cursor?
For the game I'm working on, I want the player to click on their character, and then use the mouse to draw a path of square tiles. Once the path is drawn, the character follows the path. Every time the mouse cursor moves into a new grid square, a semi-transparent "path piece" sprite is instantiated in that square, to represent the path that the character will take.
By and large, I have all of this working correctly. There's just one issue that I'm not sure how to solve: If you move the mouse very rapidly, the script sort of can't "keep up." Gaps appear in the character's drawn path. These gaps don't really affect the actual logic of what's happening due to how everything is implemented, but it looks ugly and it seems like an avoidable problem.
This is the code I have for drawing the path:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PathCreator : MonoBehaviour {
bool makingPath = false;
Vector2 prevTile = Vector2.zero;
public Queue<GameObject> pathPieces = new Queue<GameObject>();
public GameObject pathPiece;
public void setMakingPath(bool clicking){
makingPath = clicking;
}
public void setPrevTile(Vector2 pos){
prevTile = pos;
}
void FixedUpdate () {
if (Input.GetMouseButtonUp (0) || !Input.GetMouseButton (0)) {
setMakingPath (false);
}
if (makingPath) {
Vector2 currTile = new Vector2(GMScript.GM.cam.ScreenToWorldPoint(Input.mousePosition).x, GMScript.GM.cam.ScreenToWorldPoint(Input.mousePosition).y);
if (currTile.x >= (prevTile.x + 64) || currTile.x < prevTile.x){
GameObject nextPiece = (GameObject) Instantiate (pathPiece);
nextPiece.transform.parent = GMScript.GM.transform;
nextPiece.transform.localPosition = new Vector3 (currTile.x - currTile.x % 64, prevTile.y, GMScript.GM.hero.transform.localPosition.z + 1);
pathPieces.Enqueue (nextPiece);
setPrevTile (new Vector2(currTile.x - currTile.x % 64, prevTile.y));
}
if (currTile.y >= (prevTile.y + 64) || currTile.y < prevTile.y){
GameObject nextPiece = (GameObject) Instantiate (pathPiece);
nextPiece.transform.parent = GMScript.GM.transform;
nextPiece.transform.localPosition = new Vector3 (prevTile.x, currTile.y - currTile.y % 64, GMScript.GM.hero.transform.localPosition.z + 1);
pathPieces.Enqueue (nextPiece);
setPrevTile (new Vector2(prevTile.x, currTile.y - currTile.y % 64));
}
}
}
}
My only thought is that I'm doing something that's making this script run slowly, more slowly than whatever code is dictating the mouse's movement, so the script is keeping up as best as it can but occasionally skips instantiating certain pieces. That doesn't sound right though, because if that were the case I would think that the drawn path would lag behind the cursor, but still be complete. It's like the FixedUpdate() function isn't happening fast enough to accurately check the position of the mouse. The code works perfectly as long as you don't move the mouse too fast.
Any advice would be appreciated.
Answer by hexagonius · Sep 25, 2015 at 10:58 AM
think of the approach this way: "what happens, if the user moved the cursor across the whole game board in one frame (in your case in one fixed update)". your code would only know the new position but nothing in between. instead of activating on mouse over, I'd calculate the path from character to cursor. you can use a*algorithm, or just pick the closer of the two adjacent tiles around the player towards the cursor and so on.
Your answer
Follow this Question
Related Questions
Making the cursor disappear after the mouse being inactive for a set amount of time? 0 Answers
Why does my mouse cursor flicker in the Unity Editor when dragging any item? 0 Answers
Move mouse without moving cursor 0 Answers
Mouse Look Script Not Working 1 Answer
Rotate OBJECT to face mouse cursor for x/z based top down shooter 3 Answers