Advertisement
EmmyDev

Rail Attach

Dec 29th, 2022 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.VisualScripting;
  5. using UnityEngine;
  6.  
  7. namespace PathCreation.Examples
  8. {
  9.     public class PathAttach : MonoBehaviour
  10.     {
  11.         private PathFollower _railPathFollower;
  12.         private bool _thisRailActive;
  13.  
  14.         [SerializeField] int _stepsSinceLastDetached = 10;
  15.  
  16.         private Transform _playerTransform;
  17.         private PathFollower _playerPathFollower;
  18.         private Rigidbody _playerRb;
  19.         private PathCreator _pathCreator;
  20.  
  21.  
  22.         private void Awake()
  23.         {
  24.             _playerTransform = GameObject.Find("Player").transform;
  25.             _playerPathFollower = _playerTransform.GetComponent<PathFollower>();
  26.             _playerRb = _playerTransform.GetComponent<Rigidbody>();
  27.             _pathCreator = GetComponent<PathCreator>();
  28.             Debug.Log("AwakeCall");
  29.             this.enabled = false;
  30.         }
  31.  
  32.  
  33.         // Update is called once per frame
  34.         void Update()
  35.         {
  36.             if (_stepsSinceLastDetached > 10)
  37.             {
  38.                 AttachObject(_playerTransform, _pathCreator, _playerPathFollower);
  39.             }
  40.  
  41.            
  42.             if (_playerPathFollower.distanceTravelled >= _pathCreator.path.length - 1 && _thisRailActive)
  43.             {
  44.                 DetachObject(_playerTransform);
  45.             }
  46.         }
  47.  
  48.         private void FixedUpdate()
  49.         {
  50.             _stepsSinceLastDetached++;
  51.         }
  52.  
  53.         private void AttachObject(Transform objectTransform, PathCreator currentPathCreator,
  54.             PathFollower objectPathFollower)
  55.         {
  56.             objectPathFollower.speed = _playerRb.velocity.magnitude;
  57.  
  58.             objectPathFollower.distanceTravelled =
  59.                 currentPathCreator.path.GetClosestDistanceAlongPath(objectTransform.position);
  60.  
  61.             objectPathFollower.pathCreator = currentPathCreator;
  62.             if (objectPathFollower.pathCreator == currentPathCreator)
  63.             {
  64.                 _thisRailActive = true;
  65.             }
  66.         }
  67.  
  68.         public void DetachObject(Transform objectTransform)
  69.         {
  70.             _stepsSinceLastDetached = 0;
  71.  
  72.             objectTransform.rotation =
  73.                 Quaternion.Euler(new Vector3(0, objectTransform.eulerAngles.y,
  74.                     0));
  75.             _playerPathFollower.pathCreator = null;
  76.             _playerPathFollower.distanceTravelled = 0;
  77.             _thisRailActive = false;
  78.         }
  79.  
  80.         private void OnEnable()
  81.         {
  82.             AttachObject(_playerTransform, _pathCreator, _playerPathFollower);
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement