Hi,
I am creating a snowboarding racing game and I want my player to follow the waypoints I created on the tracks as AI, But I still want to be able to swipe and move the character left and right, but still follow the predefined waypoint path.

I am currently using this code to make my player follow the waypoint, but it does not allow me to move the player left and right freely. It goes back to center after moving.
#region Following the path
if (PathGenerator.instance.wayPoints.Count > 0)
{
Transform currentNode = PathGenerator.instance.wayPoints[currentNodeCount];
// transform.position = Vector3.MoveTowards(transform.position, currentNode.forward, forwardSpeed * Time.deltaTime);
transform.Translate(currentNode.transform.forward * forwardSpeed * Time.deltaTime);
float distance = Vector3.Distance(transform.position, currentNode.position);
if(distance < 0.2f)
{
if(currentNodeCount < PathGenerator.instance.wayPoints.Count - 1)
currentNodeCount++;
}
}
#endregion
Can anyone help me?
Maybe you could use a “dummy”, I mean a second invisible GO (which wouldn’t be rendered, only its Transform would be of interest).
This is not directly your player but the dummy which would follow the waypoints, leaving you the possibility to move your player relatively to the dummy.
Just an idea…
When you move left/right from the center of the path, you could save that offset as a float, than each frame you would move the player towards next waypoint and after doing transform.Translate
you could add the offset with transform.position += transform.right * Xoffset;
The below may not be the best solution to this problem, but shall help go further
You can setup the player as below
Player object with script and the character(capsule) as child
please check below image
player_heirarchy
and then with below code you will be allowed to move character side ways keeping the main object follow the waypoint
void Update()
{
//made a similar situation from your code
if (waypoints.Length > 0)
{
Transform currentNode = waypoints[currentNodeCount].transform;
transform.position = Vector3.MoveTowards(transform.position, currentNode.position, 10f * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(currentNode.position - transform.position), 1 * Time.deltaTime);
float distance = Vector3.Distance(transform.position, currentNode.position);
if (distance < 1f)
{
if (currentNodeCount < waypoints.Length - 1)
currentNodeCount++;
}
}
// side movement to child object, in m case it is Player Object's second child
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.GetChild(1).Translate(Vector3.left * 5f * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.RightArrow))
{
transform.GetChild(1).Translate(Vector3.right * 5f * Time.deltaTime);
}
}
Also attaching the output video
Follow_waypoint_along_with_manual_side_movement
P.S. : This is my first answer, please forgive if not answered in correct format.