Forum rules - please read before posting.

Change hotspot raycast length

Can i change the hotspot raycast length for an object?
I have it set at 5 in settings but i want to change it to 100 for a different object.

Comments

  • edited July 2022

    You can change it in the Settings Manager through script (right-click the field's label to copy an API reference to it), but this is a global setting. Raycasting works independently of Hotspots - it can't be set per-Hotspot.

    What you'd need to do is set your Settings Manager's Hotspot detection method to Custom Script, and then rely on a custom script to handle the raycasting. Try this:

    using UnityEngine;
    
    namespace AC
    {
    
        public class CustomRayLengths : MonoBehaviour
        {
    
            public float normalDistance = 5f;
            public float farDistance = 100f;
            public string distantHotspotTag = "DistantHotspot";
    
    
            private void Update ()
            {
                Hotspot nearestHotspot = null;
    
                if (KickStarter.settingsManager.hotspotDetection == HotspotDetection.CustomScript && KickStarter.stateHandler.IsInGameplay ())
                {
                    float nearestHotspotDistance = Mathf.Infinity;
    
                    Ray ray = Camera.main.ScreenPointToRay (KickStarter.playerInput.GetMousePosition ());
                    RaycastHit hit;
    
                    LayerMask HotspotLayerMask = 1 << LayerMask.NameToLayer (KickStarter.settingsManager.hotspotLayer);
                    if (Physics.Raycast (ray, out hit, KickStarter.settingsManager.hotspotRaycastLength, HotspotLayerMask))
                    {
                        Hotspot hitHotspot = hit.collider.gameObject.GetComponent <Hotspot>();
                        if (hitHotspot)
                        {
                            float distance = Vector3.Distance (hitHotspot.GetIconPosition (), Camera.main.transform.position);
                            if (distance <= normalDistance || (distance <= farDistance && hitHotspot.gameObject.CompareTag (distantHotspotTag)))
                            {
                                nearestHotspot = hitHotspot;
                            }
                        }
                    }
    
                    KickStarter.playerInteraction.SetActiveHotspot (nearestHotspot);
                }
            }
    
        }
    
    }
    

    Place in a C# script named CustomRayLengths, and add to your scene. Then, for any Hotspot that should be detectable 100 units away, give its GameObject a Tag named "DistantHotspot".

  • I made a script named CustomRayLengths, i add it to the scene but it gave me errors in console.

    Assets\CustomRayLengths.cs(11,42): error CS0029: Cannot implicitly convert type 'string' to 'float'
    Assets\CustomRayLengths.cs(31,89): error CS1503: Argument 2: cannot convert from 'UnityEngine.Transform' to 'UnityEngine.Vector3'
    Assets\CustomRayLengths.cs(32,120): error CS1503: Argument 1: cannot convert from 'float' to 'string'

  • Sorry, typo - try it now.

  • Hmm, it's not working, i made two objects in the scene a cube and a sphere and ad a hotspot for both, for the cube a gave it a Tag named "DistantHotspot" and place the script in the scene on an empty object, but it's not working the 100 units away it detects both of them at 5 units.

  • I forgot to change the Raycast settings to it's default, now it's working.
    Thank you.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.