Forum rules - please read before posting.

Copying the cursor's coordinates to a marker when clicked

edited July 2021 in Technical Q&A

Let's say we're in a desert and there's a screen-wide hotspot on the bottom of the screen to move to the next room downwards. Before switching scenes, I want the player character to walk down to the spot where I've clicked.

With a player action "walk to", the player just walks to the middle of the hotspot, so that won't work. With "Walk to marker", I can just setup one marker point. But what if I can make that marker a dynamic one? Is there a way to store the clicked coordinates to a global Vector3 variable and teleport the marker there before doing Character>Move to point?

Or is there another, smarter way to achieve this? Thanks beforehand!

(A friend suggested trying Object>Record transform on the ClickMarker asset, but that didn't seem to work. All I was able to do was to change the marker's coordinates to 0,0,0 so that didn't seem to grab the coordinates of the cursor position)

Comments

  • Dynamically-positioning the Walk-to Marker is the best way to go about it, yes. Though, it is worth mentioning that if you had a Trigger instead of a Hotspot, the Player would move to the click point as normal.

    A friend suggested trying Object>Record transform on the ClickMarker asset

    The issue here is that the prefab asset itself is unchanged - what you'd be looking to read is the spawned instance of it, not the original asset.

    Is this for a 3D game? The most reliable method is to perform a raycast on the NavMesh to calculate the correct point in 3D space.

    Something like this, attached to the Hotspot, should do the trick:

    using UnityEngine;
    using AC;
    
    public class RepositionHotspotMarker : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnHotspotInteract += OnInteract; }
        private void OnDisable () { EventManager.OnHotspotInteract -= OnInteract; }
    
        private void OnInteract (Hotspot hotspot, AC.Button button)
        {
            if (hotspot == GetComponent <Hotspot>())
            {
                Vector2 mousePosition = KickStarter.playerInput.GetMousePosition ();
                Ray ray = KickStarter.CameraMain.ScreenPointToRay (mousePosition);
                RaycastHit hit = new RaycastHit();
    
                if (Physics.Raycast (ray, out hit, KickStarter.settingsManager.navMeshRaycastLength))
                {
                    hotspot.walkToMarker.transform.position = hit.point;
                }
            }
        }
    
    }
    

    This works by hooking into the OnHotspotInteract custom event, and then updates the Hotspot's walk-to Marker position based on the cursor position at that moment.

  • edited July 2021

    Sorry for not clarifying the nature of the game in the initial message. This is for a 2D PNC adventure game, I just want the player character to walk where it's clicked and then begin at that point on the X axis on top of the next room. It's nicer that way and has something to do with a specific puzzle, too.

    The rest I was able to manage, but this first thing remained a problem... until now! Using a trigger instead of a hotspot was the perfect solution. Thanks a lot @ChrisIceBox! :smile:

  • edited July 2021

    For the benefit of anyone else looking to use the script in a 2D game:

    using UnityEngine;
    using AC;
    
    public class RepositionHotspotMarker : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnHotspotInteract += OnInteract; }
        private void OnDisable () { EventManager.OnHotspotInteract -= OnInteract; }
    
        private void OnInteract (Hotspot hotspot, AC.Button button)
        {
            if (hotspot == GetComponent <Hotspot>())
            {
                Vector2 mousePosition = KickStarter.playerInput.GetMousePosition ();
                Vector3 mouseWorldPosition = KickStarter.CameraMain.ScreenToWorldPoint (mousePosition);
                hotspot.walkToMarker.transform.position = new Vector3 (mouseWorldPosition.x, hotspot.walkToMarker.transform.position.y, hotspot.walkToMarker.transform.position.z);
            }
        }
    
    }
    
  • edited July 2021

    Ah, one problem remains: it was originally supposed to be a hotspot because it should be left for the player to decide to "go down", not automatically move there as the bottom part of the screen is (perhaps accidentally) clicked. But if I add a hotspot of the same size with "walk to" player action, the character stops before reaching the trigger... and I can't really make the trigger much bigger. @ChrisIceBox, is there a way to have a hotspot that doesn't really affect anything and lets the normal "walk to" -click do its thing?

  • Btw, the 2D script you provided gives this alert:
    Assets\Scripts\RepositionHotspotMarker.cs(16,13): error CS1612: Cannot modify the return value of 'Transform.position' because it is not a variable

  • My mistake - I've corrected it above.

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.