Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// TrajectoryuData.cs
- /// Calculates trajectory data from start to target position
- public static TrajectoryData CalculateTrajectory(
- Vector3 start,
- Vector3 target,
- float speed,
- float heightMultiplier,
- float rotationSpeed,
- int rotationDirection = 0)
- {
- float distance = Vector3.Distance(start, target);
- float flightTime = ProjectilePhysics.CalculateFlightTime(distance, speed);
- float maxHeight = ProjectilePhysics.CalculateArcHeight(distance, heightMultiplier);
- int rotDir = rotationDirection != 0 ? rotationDirection : (Random.value > 0.5f ? 1 : -1);
- return new TrajectoryData(start, target, flightTime, maxHeight, rotationSpeed, rotDir);
- }
- ---
- /// SkullTrajectory.cs
- void Update()
- {
- elapsedTime += Time.deltaTime;
- if (trajectoryData.IsComplete(elapsedTime))
- {
- CompleteThrow();
- return;
- }
- UpdatePosition();
- UpdateRotation();
- UpdateShadow();
- UpdateCollider();
- }
- private void UpdatePosition()
- {
- float progress = trajectoryData.GetProgress(elapsedTime);
- // Horizontal movement
- basePosition = ProjectilePhysics.CalculateBasePosition(
- trajectoryData.startPosition,
- trajectoryData.targetPosition,
- progress);
- // Vertical movement
- currentHeight = ProjectilePhysics.CalculateHeight(
- elapsedTime,
- trajectoryData.verticalVelocity,
- trajectoryData.gravity);
- // Final position
- Vector3 finalPos = ProjectilePhysics.CalculatePosition(basePosition, currentHeight);
- transform.position = finalPos;
- // Update depth sorting
- if (depthSorting != null)
- {
- depthSorting.SetHeight(currentHeight);
- }
- }
- ---
- /// ProjectilePhysics.cs
- public static float CalculateFlightTime(float distance, float speed, float timeScale = 1f)
- {
- return Mathf.Lerp(0.5f * timeScale, distance / speed, distance / 10f);
- }
- public static float CalculateArcHeight(float distance, float heightMultiplier)
- {
- return distance * heightMultiplier;
- }
- public static Vector3 CalculatePosition(Vector3 basePosition, float height)
- {
- return basePosition + Vector3.up * Mathf.Max(0, height);
- }
- public static float CalculateGravity(float maxHeight, float flightTime)
- {
- float peakTime = flightTime * 0.5f;
- return 2f * maxHeight / Mathf.Pow(peakTime, 2f);
- }
- public static Vector3 CalculateBasePosition(Vector3 start, Vector3 target, float progress)
- {
- return Vector3.Lerp(start, target, progress);
- }
- ---
- /// DepthSorting.cs
- void LateUpdate()
- {
- if (spriteRenderer != null)
- {
- float groundY = transform.position.y;
- int order = Mathf.RoundToInt(-(groundY - currentHeight) * 100f);
- spriteRenderer.sortingOrder = order;
- }
- }
- public void SetHeight(float height)
- {
- currentHeight = height;
- }
Advertisement
Add Comment
Please, Sign In to add comment