Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by GoMeteoroGo · Sep 25, 2015 at 07:21 AM · mousecursorfixedupdate

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges