Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
- namespace PathCreation.Examples
- {
- public class PathAttach : MonoBehaviour
- {
- private PathFollower _railPathFollower;
- private bool _thisRailActive;
- [SerializeField] int _stepsSinceLastDetached = 10;
- private Transform _playerTransform;
- private PathFollower _playerPathFollower;
- private Rigidbody _playerRb;
- private PathCreator _pathCreator;
- private void Awake()
- {
- _playerTransform = GameObject.Find("Player").transform;
- _playerPathFollower = _playerTransform.GetComponent<PathFollower>();
- _playerRb = _playerTransform.GetComponent<Rigidbody>();
- _pathCreator = GetComponent<PathCreator>();
- Debug.Log("AwakeCall");
- this.enabled = false;
- }
- // Update is called once per frame
- void Update()
- {
- if (_stepsSinceLastDetached > 10)
- {
- AttachObject(_playerTransform, _pathCreator, _playerPathFollower);
- }
- if (_playerPathFollower.distanceTravelled >= _pathCreator.path.length - 1 && _thisRailActive)
- {
- DetachObject(_playerTransform);
- }
- }
- private void FixedUpdate()
- {
- _stepsSinceLastDetached++;
- }
- private void AttachObject(Transform objectTransform, PathCreator currentPathCreator,
- PathFollower objectPathFollower)
- {
- objectPathFollower.speed = _playerRb.velocity.magnitude;
- objectPathFollower.distanceTravelled =
- currentPathCreator.path.GetClosestDistanceAlongPath(objectTransform.position);
- objectPathFollower.pathCreator = currentPathCreator;
- if (objectPathFollower.pathCreator == currentPathCreator)
- {
- _thisRailActive = true;
- }
- }
- public void DetachObject(Transform objectTransform)
- {
- _stepsSinceLastDetached = 0;
- objectTransform.rotation =
- Quaternion.Euler(new Vector3(0, objectTransform.eulerAngles.y,
- 0));
- _playerPathFollower.pathCreator = null;
- _playerPathFollower.distanceTravelled = 0;
- _thisRailActive = false;
- }
- private void OnEnable()
- {
- AttachObject(_playerTransform, _pathCreator, _playerPathFollower);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement