- Home /
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;
}
}
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141723im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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