Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by ajatx.89 · Apr 02, 2014 at 01:14 AM · pathmouse-dragfollow path

Draw path to follow with mouse problems

I need help with my code. So far, it works okay. The player follows the mouse path as expected, but the player moves at the same speed as how fast or how slow you drag the mouse. Please help! I need the player to move at a normal and controlled speed regardless of how fast or slow I drag the mouse. I'm posting my code here for a reference. Thank you.

 using UnityEngine;
 using System.Collections;
 
 public class FollowMousePath : MonoBehaviour {
 
     public float[] posXArray;
     public float[] posYArray;
 
     float startX;
     float startY;
 
     public float nextStepX;
     public float nextStepY;
     public float mySpeed = 45;
     public float recordTime;
 
     public Vector3 previousPos;
     public Vector3 mousePos;
     public Vector3 endPos;
 
     public int whichArrayToRecord;
     public int myMoves;
     public int maxMoves;
 
     public bool playerGo;
     public bool recordPos;
     public bool beginRecording;
 
     // Use this for initialization
     void Start () {
         posXArray = new float[300];
         posYArray = new float[300];
     }
     
     // Update is called once per frame
     void Update () {
 
         mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
         if (Input.GetMouseButtonDown (0)) {
             beginRecording = true;
             recordTime = 0.0001f;
         }
 
         if (Input.GetMouseButtonUp (0)) {
             maxMoves = whichArrayToRecord;
             beginRecording = false;
             whichArrayToRecord = 0;
             playerGo = true;
             myMoves = myMoves + 1;
         }
 
         if (playerGo && myMoves < maxMoves) {
             nextStepX = posXArray[myMoves];
             nextStepY = posYArray[myMoves];
             transform.position = new Vector3(Mathf.MoveTowards(transform.position.x, nextStepX, mySpeed * Time.deltaTime),Mathf.MoveTowards(transform.position.y, nextStepY, mySpeed * Time.deltaTime), transform.position.z);
             myMoves += 1;
         }
 
         if (myMoves == maxMoves) {
             myMoves = 1;
             playerGo = false;
         }
         
         if (mousePos != previousPos && beginRecording) {
             previousPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             endPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             posXArray[whichArrayToRecord] = endPos.x;
             posYArray[whichArrayToRecord] = endPos.y;
             whichArrayToRecord +=1;
             recordTime = 0.0001f;
             recordPos = false;
         }
     
         if (recordPos && Input.GetMouseButtonUp(0)) {
             endPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             recordPos = false;
             playerGo = true;
         }
 
         if (endPos.z != 0)
             endPos.z = 0;
 
     }
 }
Comment
Add comment · Show 1
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
avatar image getyour411 · Apr 02, 2014 at 01:21 AM 0
Share

It looks like your previousPos and endPos are based on Input.mousePosition; I didn't read all this but if that's true and you don't want player movement to be tied to mouse movement, then undo that.

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Itween change paths at runtime? 1 Answer

GameObject to follow another GameObject through a spline with a dynamic end waypoint 0 Answers

Orientation along spline path / Mesh extrusion along path, problem with smooth orientation 1 Answer

Make a drunk character swerve and loop on the way to a waypoint but still make it eventually. 2 Answers

iTweens MoveTo and "path" property 7 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