0
\$\begingroup\$

I have this script which calculates waveHeight based on a raycastHit point.

Right now it only casts the raycastHit from observer.transform.position, so therefore it only ever returns waveHeight from observer's position. Instead, I need the functionality of having this script return waveHeight from any position queried from an outside script. Therefore, I started to create the public float WaterHeightAtPosition which takes in a Vector3 position, but I don't know how to continue coding this from there.

And so, how can I make public float WaterHeightAtPosition return waveHeight based on any outside gameObject's position? That way, if an outside script enters something like GetWaterHeight.WaterHeightAtPosition(transform.position), then it will indeed return the correct number based on that outside gameObject's transform?

Thank you for any help.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GetWaterHeight : MonoBehaviour
{
    public GameObject observer;
    public LayerMask objectLayer;

    public float rayHeight = 20f;
    RaycastHitRenderer hit;
    Ray ray;

    public float waveHeight = 0;

    void Update()
    {
        ray = new Ray(observer.transform.position, Vector3.up);
        Debug.DrawRay(observer.transform.position, Vector3.up * rayHeight, Color.red);
    }

    private void FixedUpdate()
    {
        print("This is it: " + waveHeight);

        waveHeight = hit.point.y;

        if (transform.position.y < waveHeight)
        {
            if (SuperRaycast.Raycast(ray, out hit, 9000f, objectLayer))
            {
                print("Floating");
            
                Debug.Log(hit.renderer.name + "      " + hit.point.y);
            }
        }
    }

    public float WaterHeightAtPosition(Vector3 position)
    {
        return waveHeight;
    }
}
\$\endgroup\$
1

0

You must log in to answer this question.

Browse other questions tagged .