- Home /
Make player follow waypoints, but still control it's X axis
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?
Answer by Koyemsi · May 03, 2021 at 05:14 PM
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...
Answer by Noctre · May 03, 2021 at 05:23 PM
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;
That makes sense, and is probably simpler than my answer ;)
I tried this but it does not work, the issue is the path is not always straight and it curves many times in a separate direction.
Answer by hirenkatira · May 06, 2021 at 03:40 PM
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
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.
Your answer
Follow this Question
Related Questions
How To Add AI To Car/Racer? 4 Answers
Enemy go haywire after player approaching it, how to properly implement waypoint and chase player. 1 Answer
Distribute terrain in zones 3 Answers
Car- can someone help me convert this to C#? 1 Answer
On waypoints if i set the moving speed of player to 20-30 he fall out of terrain why ? 0 Answers