Path Movement created from user input.mouse and player follows it.
I am new to Unity and have not programmed in years... With that I only knew very simple C++ and GameMakerLanguage (GML). I realize my coding may have errors or even "no no"s... please feel free to point them out! haha. This is my first post in Unity Answers. Thanks in advance for anyone's help.
So.. I have the basic foundation of this and it all seems to be working fairly well. Though my problem occurs when my player reaches his first "WayPoint", and continues in the direction of the second. It seems that it begins to move towards the correct X value but the opposite direction of the Y value.
What I have thus far.. Basic outline of the code below:
User click and holds going up, making a zig zag form.
Function takes all coordinates and checks if the mouse went left, right and up past a specific point. If it passes, then call the "PathBurst(); function".
All the mouse coordinates were put into a list called "touchPathx".
The touchPathx coordinates are all checked in a for loop through.
Each coordinate in the list is checked with the previous coordinate. If the coordinate is less than the previous, continue searching, otherwise add the X and Y value to a new List called "pathBurstCBx".
The touchPathx continues to compare each previous coordinate in the for loop, but this time asking if it is greater than the previous. If it is greater, continue searching, otherwise ass the X and Y value to the pathBurstCBx list.
This continues to allow the pathBurstCBx list to have a max length of 7. (0-6). The if statement controls that.
To ensure all these points are being added correctly from screen to world, I put them into Vector3s with a Z value of 10.
I also print each taken coordinate that passes into the pathBurstCBx list in the Debug.Log.
This all works fine! Or at least it seems to work okay.
I then have the player take the list "pathBurstCBx" and move towards each coordinate, similar to WayPoints.
The problem occurs after passing the first coordinate. It seems the player travels to the first x and y correctly, then when it switches to pathBurstCBx[1] (the second coordinate), it travels in the correct X direction but not the correct Y. It in fact goes the opposite direction of Y.
I then decided to print the position of the player to show where it is moving. It goes right up to the first coordinate, and then switches to the second, then gets stuck in the middle of the air, or falls, or goes the opposite direction.
Any help would be greatly appreciated! Look at these pictures and code for reference. Thanks!
Also, I'm not sure if it matters or not, but there is gravity set to -13 in this scene.
This image shows the Debug Log with the points that were taken and assigned. In which the player will move to.
This image shows the Debug Log with the players position. You can visually see the player moving towards the first coordinate. Then it switches and begins to move/continues to move in the wrong direction. I feel like this might be something silly that I am not realizing, like the world Y axis is -1 in the world but +1 on screen? After this, the player just continues to fall with gravity. I tired setting useGravity to false while this happens, but it doesn't do anything. The gravity would make it fall before it even reached the first point. Right?
Code:
#region Path Burst
void PathBurst()
{
//ask if the pathBursting variable is false. If so, set all the data points.
if (pathBursting == false)
{
//start searchinging the list of touch coordinates and assign the first check, to the coordinates of the first touch X value.
checkTouchMotioni = startTouch.x;
for (int i = 0; i < touchPathx.Count; i++)
{
// Debug.Log("All Points: " + Camera.main.ScreenToWorldPoint(new Vector3(touchPathx[i], touchPathy[i], 10)));
//Only allow the new list to have 7 points (0-6)
if (ii < 7)
{
//Prepare the screen coordinate for the world coordinates
Vector3 tempST = Camera.main.ScreenToWorldPoint(startTouch);
Vector3 pBS = Camera.main.ScreenToWorldPoint(new Vector3(touchPathx[i], touchPathy[i], 10));
//check if the new list index is even or odd, to alternate left and right
if (ii % 2 == 0) //first coordinates added will be in the (0)
{
if (touchPathx[i] < checkTouchMotioni) //if the current X coordinate being checked is less than the previous
{
//then keep searching
checkTouchMotioni = touchPathx[i];
}
else //if the X coordinate being checked is not less than the previous, then stop searching and set that value X to the new List.
{
//Add the data point for the Path
pathBurstCBx.Add(new Vector3(rbBall.transform.position.x - (tempST.x - pBS.x), rbBall.transform.position.y + (tempST.y - pBS.y), 0));
//Print what data point is being set and where it is.
Debug.Log("Point : " + ii + " : " + pathBurstCBx[ii]);
//continue the search for the next point.
checkTouchMotioni = touchPathx[i];
//To ensure the list won't be larger than 7 points and to check which position will be changed in the List, increase the ii variable by 1
ii += 1;
}
}// if the new list index is odd, perform the same tasks previously commented, but comparing if it is greater than.
else if (ii % 2 == 1)
{
if (touchPathx[i] > checkTouchMotioni)
{
checkTouchMotioni = touchPathx[i];
}
else
{
//Add the data point for the Path
pathBurstCBx.Add(new Vector3(rbBall.transform.position.x - (tempST.x - pBS.x), rbBall.transform.position.y + (tempST.y - pBS.y), 0));
Debug.Log("Point : " + ii + " : " + pathBurstCBx[ii]);
checkTouchMotioni = touchPathx[i];
ii += 1;
}
}
}
else // if the counter for the new list is not less than 7, assign the X coordinate and break the for loop.
{
Vector3 tempST = Camera.main.ScreenToWorldPoint(startTouch);
Vector3 pBS = Camera.main.ScreenToWorldPoint(new Vector3(touchPathx[i], touchPathy[i], 10));
pathBurstCBx.Add(new Vector3(rbBall.transform.position.x - (tempST.x - pBS.x), rbBall.transform.position.y + (tempST.y - pBS.y), 0));
Debug.Log("Point : " + ii + " : " + pathBurstCBx[ii]);
break;
}
}
}
//All data points have been set successfully into a list.
#region Path Movement
//ask if the pathBursting variable is false. If so, set it to true to avoid going through the previously established data points.
if (pathBursting == false)
{
//set pathBursting to true, and reset a counter variable "cBcounter" for knowing which X coordinate we will be using from the list.
pathBursting = true;
cBcounter = 0;
Debug.Log("Start PathBurst!");
Debug.Log("Path Point : " + cBcounter); //We will be using the X coordinate in the 0 place, in the pathBurstCBx list.
}
//check if pathBursting is true, so it doesn't continue to move if the movement phase has ended.
if (pathBursting == true)
{
//check if the counter variable is less than the total X coordinate data points we collected. Which should be 7.
if (cBcounter < pathBurstCBx.Count)
{
//Create a new Vector3 for the position we want the player to be facing, relative to the player.
//The coordinates are so miniscule after conversion from screen to world, multiply the wanted destination by 5.
Vector3 relativePos = pathBurstCBx[cBcounter] * 5 - rbBall.transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos); //rotate the object to point int he correct direction just established.
//to be sure the player doesnt fly off the screen at an extreme rate, limit the speed.
if (actualSpeed < 20)
{
//set actualSpeed equal to itself, times an acceleration varaible.
actualSpeed = actualSpeed + accel * accel;
}
//set the position of the player using the movetowards funnction, and don't forget to multiply it by 5, just as in the position rotation.
rbBall.transform.position = (Vector3.MoveTowards(transform.position, pathBurstCBx[cBcounter] * 5, Time.deltaTime * actualSpeed));
//print to the console the exact position of the player to compare to the data point previously printed.
Debug.Log("Direction: " + (Vector3.MoveTowards(transform.position, pathBurstCBx[cBcounter] * 5, Time.deltaTime * actualSpeed)));
//if the position of the player is less than .5 length away from the desired position (the x and Y coordinate in the list), then move on to the next coordinate in the list.
if ((rbBall.transform.position - pathBurstCBx[cBcounter]).magnitude < 0.5f)
{
//increase counter variable to move on to the next coordinate in the list
cBcounter++;
//print to console what position is desired now
Debug.Log("Path Point : " + cBcounter);
}
#endregion
}
else //if the counter variable reached the end, then reset the variables for pathbursting and set it to false.
{
cBcounter = 0;
pathBursting = false;
ii = 0;
}
}
}
#endregion