Your Cart
Loading
unity3d waypoint movement unity waypoint system unity waypoint tutorial

Unity3D Waypoint Movement | Unity Tutorial

Unity3D Waypoint Movement | Unity Tutorial


In this video tutorial, we’ll show you how to set up a waypoint system in Unity to control player movement along a predefined path. You’ll learn how to create and position waypoint markers in your scene, write a script to move the player character from one waypoint to the next, and handle smooth transitions between waypoints. By the end of the tutorial, you'll have a player movement system that navigates through a series of points, perfect for creating guided paths or automated sequences in your game. Ideal for Unity beginners, this tutorial provides clear, step-by-step instructions to enhance your game with controlled player movement.



WATCH FULL TUTORIAL ON YOUTUBE



CHECK OUR PROJECTS


SUBSCRIBE FOR LATEST OFFERS



=========================================================


3 Unity Projects Only $50 - ON SALE!!!!

https://youtu.be/7mI-fJpXN88


Contact: unitycoderz2022@gmail.com


=========================================================



Scripts:


WayPointMovement.cs


using UnityEngine;

using System.Collections;


public class WayPointMovement : MonoBehaviour

{

  public Transform[] waypoints;     // Array of waypoints

  public float speed = 2f;       // Patrol speed

  public float waitTime = 2f;      // Time to wait at each waypoint

  public float groundCheckDistance = 0.2f; // Distance to check for ground

  public LayerMask groundLayer;     // Layer mask to specify ground layers


  private int currentWaypointIndex = 0;

  private Rigidbody rb;

  private bool isWaiting = false;

  private bool isGrounded;


  void Start()

  {

    rb = GetComponent<Rigidbody>();

    rb.freezeRotation = true; // Prevent rotation

    rb.useGravity = true;   // Enable gravity

    if (waypoints.Length > 0)

    {

      StartCoroutine(Patrol());

    }

  }


  void FixedUpdate()

  {

    // Check if the enemy is grounded using the LayerMask

    isGrounded = Physics.Raycast(transform.position, Vector3.down, groundCheckDistance, groundLayer);


    // Apply manual adjustment to prevent unrealistic behavior

    if (!isGrounded)

    {

      Vector3 adjustedVelocity = rb.velocity;

      adjustedVelocity.y = 0; // Prevent vertical movement

      rb.velocity = adjustedVelocity;

    }

  }


  private IEnumerator Patrol()

  {

    while (true) // Infinite loop for continuous patrolling

    {

      if (waypoints.Length == 0) yield break;


      Transform targetWaypoint = waypoints[currentWaypointIndex];

      Vector3 direction = (targetWaypoint.position - transform.position).normalized;


      // Move the enemy manually

      Vector3 movement = direction * speed * Time.fixedDeltaTime;

      rb.velocity = Vector3.zero; // Reset velocity before applying movement

      rb.MovePosition(rb.position + movement);


      // Check if we've reached the waypoint

      if (Vector3.Distance(transform.position, targetWaypoint.position) < 0.1f)

      {

        if (!isWaiting)

        {

          StartCoroutine(WaitAtWaypoint());

        }

      }


      yield return new WaitForFixedUpdate(); // Wait until the next FixedUpdate

    }

  }


  private IEnumerator WaitAtWaypoint()

  {

    isWaiting = true;

    yield return new WaitForSeconds(waitTime); // Wait for the specified time

    isWaiting = false;


    // Move to the next waypoint

    currentWaypointIndex = (currentWaypointIndex + 1) % waypoints.Length;

  }

}


-----------------------------------------------------------------------------------------------------------------------------------------


CameraFollowPlayer.cs



using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class CameraFollowPlayer : MonoBehaviour

{

  public Transform player;    // The player's transform

  public Vector3 offset;     // Offset position from the player

  public float smoothSpeed = 0.125f; // Speed at which the camera follows


  void LateUpdate()

  {

    if (player != null)

    {

      // Calculate the desired position

      Vector3 desiredPosition = player.position + offset;

      // Smoothly interpolate between the camera's current position and the desired position

      Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);

      // Update the camera's position

      transform.position = smoothedPosition;

    }

  }

}





DOWNLOAD CODESTER FILES