- Home /
Make player follow a waypoint path but still be able to control player movement
Hi, I am developing a racing game where I have a predefined path with waypoints, and what I want to achieve is, I want my player to follow that general direction of the path but still be in full control of my player. As in, it keeps following the path, But I should be able to move it left and right to avoid obstacles.
Here is the piece of code I use right now.
  #region Following the path
         if (PathGenerator.instance.wayPoints.Count > 0)
         {
             Transform currentNode = PathGenerator.instance.wayPoints[currentNodeCount];
 
             transform.position = Vector3.MoveTowards(transform.position, currentNode.position, forwardSpeed * 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 < PathGenerator.instance.wayPoints.Count - 1)
                 currentNodeCount++;
             }
         }
         #endregion
But the issue here Is that the player goes directly to the waypoint, even if I move it left and right. It would just go back to the center of the waypoint.
watch the gif:- https://s3.gifyu.com/images/Demo-00_00_00-00_00_30-640i.gif (Sorry could not upload here because of upload limitations in Unity Answers)
As you can see how it comes back to the center to follow the waypoint, I can't control the player like this. All I want is the player to follow the circuit path itself, but I want to control it's X-axis in order to avoid obstacle.
Can someone please guide me here and help me solve my problem? Any help or idea would be appreciated.
Thank you.
Answer by TeamON · May 06, 2021 at 01:28 PM
Just add Vector3 offsetPosition like
 transform.position = Vector3.MoveTowards(transform.position, currentNode.position, forwardSpeed * Time.deltaTime) + offsetPosition;
Where the offsetPosition variable changes its value from the player's control
Your answer
 
 
             Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Why isn't a Ai Waypoint Spawning??? 0 Answers
Multiple Cars not working 1 Answer
Make player follow waypoints, but still control it's X axis 3 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                