Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Question How to make player move along the spline/bezier curve

Discussion in 'Scripting' started by skynote07, Jun 1, 2023.

  1. skynote07

    skynote07

    Joined:
    May 10, 2022
    Posts:
    3
    Hello, currently I'm doing a project where player should be moving along separate lines and I've used Bezier Curve package to create the paths that I want the players to move along and it looks like a curved 'T' shape with 2 separate splines (since I'm using 2021LTS version it doesn't have splines)
    https://assetstore.unity.com/packages/tools/utilities/b-zier-path-creator-136082

    Is there a way to make player move along the bezier curve and once player meets the intersection of the other curve can we let the player to move along either one of bezier curve? The code that I currently have is in below:
    Code (CSharp):
    1. using UnityEngine;
    2. using PathCreation;
    3.  
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.     private PlayerController playerControls;
    7.     private CharacterController controller;
    8.     private Vector3 playerVelocity;
    9.  
    10.     [SerializeField]
    11.     private float playerSpeed = 2.0f;
    12.     private InputManager inputManager;
    13.     private Transform cameraTransform;
    14.     //should this be array?
    15.     public PathCreator path;
    16.  
    17.  
    18.     private void Start()
    19.     {
    20.         controller = GetComponent<CharacterController>();
    21.         inputManager = InputManager.Instance;
    22.         cameraTransform = Camera.main.transform;
    23.     }
    24.  
    25.     //Movement Control
    26.     private void Move(){
    27.         Vector2 movement = inputManager.GetPlayerMovement();
    28.         Vector3 move = new Vector3(movement.x, 0f, movement.y);
    29.         move = cameraTransform.forward * move.z + cameraTransform.right * move.x;
    30.  
    31.         controller.Move(move * Time.deltaTime * playerSpeed);
    32.  
    33.         if (move != Vector3.zero)
    34.         {
    35.             gameObject.transform.forward = move;
    36.         }
    37.  
    38.         controller.Move(playerVelocity * Time.deltaTime);
    39.     }
    40.  
    41.     void Update()
    42.     {
    43.         Move();
    44.     }
    45. }