Using Bezier Curves for the Character to take curved Path.
- santosh nalla
- Mar 26, 2020
- 3 min read
Ever tried to develop an infinity runner game in Unity? Here, you usually transform your player forward and you keep generating the path as the player goes forward. You add features like changing lanes, jumping, ducking etc.
Now think of how you are gonna make your player move forward? Most of us usually update player position forward every frame.
void PlayerMotion() { transform.position += transform.forward * _playerSpeed * Time.deltaTime; }
This is what most of the tutorials on Youtube teach you. This works really fine when the path is straight. But what if the path gets curved on its way? How will you make your player follow the curved path? Because here the PlayerMotion() function doesn't work, because it only deals when the path is straight.
Now to overcome this difficulty, Bezier Curves are used.
Firstly, this plugin has many uses. It is a free plugin in Unity store - "Bezier Solution". Download this plugin first.
Now how to use that?
Firstly, create an empty game object and add "Bezier Spline" script to that empty game object. As soon as you add that script, you get a curve with 2 default points. Note that you can add as many points as you can to match the path curve. Try to add required number of points to match the path curve with the bezier curve you got in your scene. Place that curve on the curved path so that both are at same height.
There are so many options to explore in that curve for you, so just play with those options to get an idea of how they completely work.


Now that you have your Bazier Curve on your curved path, How can we make an object/Character follow the path. This is pretty simple, just drag and drop "BezierWalkerWithSpeed" script on to your character. Once you activate that script, character starts to follow that path.
Hm, now the trick here is, as soon as you activate that script on the character, character will start to take the path of the curve. So initially you have to disable that script. Add a collider at the start of the curve(Point where the path takes curve). Once the character collides with the collider, enable that script and once the character exits the curve, add an other collider and disable the script.
Below is the image with 2 colliders, tagged "Entry" and "Exit"

Now , as explained in the above, Activate the "BezierWalkerWithSpeed" script which is attached to the character, when the character collides with the entry Collider. Deactivate the "BezierWalkerWithSpeed" script when the character collides with the exit collider.
Given below is the complete script attached to the player/Character.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SantoshBlog
{
public class Player : MonoBehaviour
{
#region PLAYER RELATED VARIABLES
[Header("PLATED RELATED")]
[SerializeField] float _playerSpeed;
#endregion
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
PlayerMotion();
}
#region CUSTOM METHODS
void PlayerMotion()
{
transform.position += transform.forward * _playerSpeed * Time.deltaTime;
}
#endregion
#region TRIGGERS AND COLLIDERS
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Entry")
{
transform.GetComponent<BezierSolution.BezierWalkerWithSpeed>().enabled = true;
}
if(other.gameObject.tag == "Exit")
{
transform.GetComponent<BezierSolution.BezierWalkerWithSpeed>().enabled = false;
}
}
#endregion
}
}

FINAL RESULT:
Thanks you and drop me a mail if you are having any queries.
Happy coding!! :)
Comentarios