I would like to be able to grind in either direction along a grindable surface using…
- User
I would like to be able to grind in either direction along a grindable surface
using UnityEngine; using UnityEngine.UI;
public class HoverboardGrindController : MonoBehaviour { [Header("Grind UI")] public Slider grindBalanceSlider; // Assign the slider in the inspector
code[Header("Grind Detection")] public float detectionRadius = 5f; public LayerMask grindableLayer; public KeyCode grindKey = KeyCode.G; // Key to start grinding public GrindableSurface currentGrindSurface; private bool isGrinding; // Update method (includes key press logic) private void Update() { // Check for grind key press and toggle grinding if (Input.GetKeyDown(grindKey)) { currentGrindSurface = FindClosestGrindableSurface(); if (currentGrindSurface != null) { StartGrinding(currentGrindSurface); } } else if (Input.GetKeyUp(grindKey)) { EndGrinding(); } if (isGrinding) { GrindAlongSurface(); // HandleGrindBalance(); // You can implement balance functionality here if needed } } // Method to find the nearest grindable surface public GrindableSurface FindClosestGrindableSurface() { Collider[] hits = Physics.OverlapSphere(transform.position, detectionRadius, grindableLayer); GrindableSurface closestSurface = null; float closestDistanceSqr = Mathf.Infinity; Vector3 currentPosition = transform.position; foreach (Collider hit in hits) { GrindableSurface surface = hit.GetComponent<GrindableSurface>(); if (surface != null) { Vector3 directionToSurface = hit.transform.position - currentPosition; float dSqrToSurface = directionToSurface.sqrMagnitude; if (dSqrToSurface < closestDistanceSqr) { closestDistanceSqr = dSqrToSurface; closestSurface = surface; } } } return closestSurface; } // Method to start grinding public void StartGrinding(GrindableSurface grindSurface) { isGrinding = true; transform.position = grindSurface.startPoint.position; // Snap to start point transform.forward = grindSurface.GetDirection(); // Face in the direction of the grind grindBalanceSlider.gameObject.SetActive(true); // You can run additional logic here (like animations, scoring system, etc.) } // Method to end grinding public void EndGrinding() { isGrinding = false; currentGrindSurface = null; grindBalanceSlider.gameObject.SetActive(false); // You can run additional logic here (like animations, scoring system, etc.) } // Method to handle movement along the surface while grinding private void GrindAlongSurface() { if (currentGrindSurface != null) { transform.position += currentGrindSurface.GetDirection() * currentGrindSurface.GetSpeed() * Time.deltaTime; // Here you could add checks to see if the grind has reached the end of the surface // And additional logic such as effects, scoring, and so on } } - Assistant