Paste
Pasted as C# by TribitX ( 2 years ago )
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;
public class DragAndShoot : NetworkBehaviour
{
private static List<DragAndShoot> activePucks = new();
private static List<DragAndShoot> draggedPucks = new();
private bool isDragging = false;
private Vector2 dragStartPosition;
private Rigidbody2D rb;
private SpriteRenderer spriteRenderer;
public float maxDragDistance;
public float shootForceMultiplier;
public Gradient arrowColorGradient;
public LineRenderer arrowRenderer;
private Button shootButton;
private GameObject fieldObject;
public override void OnNetworkSpawn()
{
if (!IsOwner)
{
Destroy(this);
}
// Add this script to the list when it starts
activePucks.Add(this);
}
void Start()
{
rb = GetComponent<Rigidbody2D>();
arrowRenderer.positionCount = 2;
arrowRenderer.startWidth = 0.1f;
arrowRenderer.endWidth = 0.1f;
shootButton = GameObject.FindGameObjectWithTag("Launch").GetComponent<Button>();
// Hide the button initially
shootButton.gameObject.SetActive(false);
// Subscribe to the button click event
shootButton.onClick.AddListener(OnShootButtonClick);
spriteRenderer = GetComponent<SpriteRenderer>();
fieldObject = GameObject.FindGameObjectWithTag("Field");
}
private void OnDestroy()
{
// Remove this script from the list when the object is destroyed
activePucks.Remove(this);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
// Check if the mouse click is over the object with the collider
Collider2D collider = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition));
if (collider != null && collider.gameObject == gameObject)
{
isDragging = true;
dragStartPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
}
if (isDragging)
{
Vector2 currentDragPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float dragDistance = Mathf.Min(Vector2.Distance(dragStartPosition, currentDragPosition), maxDragDistance);
// Ensure the arrow is drawn only up to the maxDragDistance
Vector2 dragDirection = currentDragPosition - dragStartPosition;
dragDirection = Vector2.ClampMagnitude(dragDirection, maxDragDistance);
currentDragPosition = dragStartPosition + dragDirection;
// Update LineRenderer positions
arrowRenderer.SetPosition(0, transform.position);
arrowRenderer.SetPosition(1, currentDragPosition);
// Calculate color based on drag distance
float t = Mathf.InverseLerp(0f, maxDragDistance, dragDistance);
Color color = arrowColorGradient.Evaluate(t);
// Set start and end color of the LineRenderer
arrowRenderer.startColor = color;
arrowRenderer.endColor = color;
if (!draggedPucks.Contains(this))
{
draggedPucks.Add(this);
}
}
if (Input.GetMouseButtonUp(0) && isDragging)
{
isDragging = false;
}
if (draggedPucks.Count == activePucks.Count && activePucks.Count != 0)
{
shootButton.gameObject.SetActive(true);
}
CheckAndDestroyIfOutOfField();
}
// Add this method to your class
[ServerRpc(RequireOwnership = false)]
void ShootPuckServerRpc(Vector2 dragEndPosition)
{
Debug.Log("It's being called clientside");
Vector2 dragDirection = dragEndPosition - dragStartPosition;
float shootForce = Mathf.Clamp(dragDirection.magnitude * shootForceMultiplier, 0f, maxDragDistance * shootForceMultiplier);
rb.AddForce(dragDirection.normalized * shootForce, ForceMode2D.Impulse);
// Reset LineRenderer positions and color
arrowRenderer.SetPosition(0, Vector3.zero);
arrowRenderer.SetPosition(1, Vector3.zero);
arrowRenderer.startColor = Color.white;
arrowRenderer.endColor = Color.white;
}
// Modify the OnShootButtonClick method to call the ShootPuckServerRpc method
void OnShootButtonClick()
{
Vector2 dragEndPosition = arrowRenderer.GetPosition(1); // Get the end position of the arrow
ShootPuckServerRpc(dragEndPosition);
draggedPucks.Clear();
// Hide the button when clicked
shootButton.gameObject.SetActive(false);
}
void CheckAndDestroyIfOutOfField()
{
Collider2D fieldCollider = fieldObject.GetComponent<Collider2D>();
float overlapPercentage = GetOverlapPercentage(fieldCollider);
if (overlapPercentage < 0.5f)
{
StartCoroutine(FadeAndDestroyCoroutine());
}
}
float GetOverlapPercentage(Collider2D fieldCollider)
{
Bounds puckBounds = spriteRenderer.bounds;
Bounds fieldBounds = fieldCollider.bounds;
float overlapArea = Mathf.Max(0f, Mathf.Min(puckBounds.max.x, fieldBounds.max.x) - Mathf.Max(puckBounds.min.x, fieldBounds.min.x)) *
Mathf.Max(0f, Mathf.Min(puckBounds.max.y, fieldBounds.max.y) - Mathf.Max(puckBounds.min.y, fieldBounds.min.y));
float puckArea = puckBounds.size.x * puckBounds.size.y;
return overlapArea / puckArea;
}
IEnumerator FadeAndDestroyCoroutine()
{
float fadeDuration = 1f; // Adjust as needed
float elapsedTime = 0f;
Color originalColor = spriteRenderer.color;
while (elapsedTime < fadeDuration)
{
float t = elapsedTime / fadeDuration;
spriteRenderer.color = Color.Lerp(originalColor, new Color(originalColor.r, originalColor.g, originalColor.b, 0f), t);
elapsedTime += Time.deltaTime;
yield return null;
}
// Destroy the object after fading
Destroy(gameObject);
}
}
Revise this Paste
Children: 127122