Certainly! Below you will find an example Unity script that incorporates raycasting for detecting objects, invoking a UnityEvent upon detection, changing the color of a gizmo when an object is detected, and using a LayerMask for raycast checks. It also includes functionality for smooth step interpolation using a lerp function.
using UnityEngine;
using UnityEngine.Events;
public class RaycastController : MonoBehaviour
{
public LayerMask layerMask; // The LayerMask for identifying which layers the raycast should detect.
public UnityEvent onRaycastHit; // Event to be invoked when the raycast hits something.
public Color gizmoColor = Color.green; // The default color for the gizmo.
public float stepHeight = 0.5f; // The height to step up.
public float stepSpeed = 5f; // The speed for smooth interpolation during stepping.
private Color originalGizmoColor; // Backup of the original gizmo color.
private Vector3 targetPosition; // The target position to achieve smooth stepping.
private bool isStepping = false; // Check if currently stepping.
void Start()
{
originalGizmoColor = gizmoColor;
targetPosition = transform.position;
}
void Update()
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
{
gizmoColor = Color.red; // Change gizmo color to red upon detection.
onRaycastHit.Invoke(); // Invoke the UnityEvent.
}
else
{
gizmoColor = originalGizmoColor;
}
HandleCharacterStepping();
}
void HandleCharacterStepping()
{
if (isStepping)
{
if (Vector3.Distance(transform.position, targetPosition) > 0.01f)
{
transform.position = Vector3.Lerp(transform.position, targetPosition, stepSpeed * Time.deltaTime);
}
else
{
transform.position = targetPosition;
isStepping = false;
}
}
if (Input.GetKeyDown(KeyCode.Space)) // Example condition to trigger a step up.
{
StartStepping();
}
}
void StartStepping()
{
targetPosition = new Vector3(transform.position.x, transform.position.y + stepHeight, transform.position.z);
isStepping = true;
}
void OnDrawGizmos()
{
Gizmos.color = gizmoColor;
Gizmos.DrawLine(transform.position, transform.position + transform.forward * 10);
}
}
Explanation:
-
Raycasting:
- A
Ray is created and cast forward from the object's position.
- If the ray hits an object on the specified
layerMask, the gizmo color changes to red and the onRaycastHit event is invoked.
-
Gizmo Color:
- The gizmo color changes based on whether an object is detected by the raycast.
-
Step Interpolation:
- When stepping is triggered (using an example
KeyCode.Space input), the StartStepping function calculates the new target position.
- The
HandleCharacterStepping function smoothly interpolates the character's position towards the target using Vector3.Lerp.
-
Gizmos:
- A green line gives visual feedback in the Editor showing the direction and length of the ray.
To utilize this script, attach it to a GameObject, assign the appropriate LayerMask, and set up any desired UnityEvents in the Inspector.
This script is a foundational starting point. Feel free to customize and adapt it further to your specific requirements!