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)
It looks like you're new here. If you want to get involved, click one of these buttons!
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.
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:
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.
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!
For the benefit of anyone else looking to use the script in a 2D game:
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.