Forum rules - please read before posting.

Pathfinding for player using First Person Movement method

Hi,

I'm trying to move my Player prefab using pathfinding, but also have the Movement method set to First Person where the cursor is locked to the center of the screen and the mouse can rotate the camera to look around (like the setup in the AC First Person game tutorial: ).

Pathfinding for the player doesn't seem to work while the movement method is set to First Person, only Point and Click seems to work. However, with Point and Click the camera doesn't rotate like in First Person. I tried using a custom camera script to get rotation while in Point and Click movement, but am running into issues with pathfinding working going that route. Ideally getting pathfinding to work in First Person movement would be my preference. Does anyone have any suggestions on how to do this?

Thanks

Comments

  • I'm not clear - are you looking to rely on point-and-click movement in First Person, or just use Character: Move to point Actions to move the Player?

    The latter should work with First Person movement provided that the NavMesh is set up, and that the Action has Wait until finish? checked.

    You may be interested in the "Movement template: Nodes" package on the Downloads page, as allows for point-to-point first person movement.

  • edited January 2020

    Sorry I wasn't clear. I'm actually trying to set it up so the player can click on the ground and move to that point they clicked on, but still be able to look around like the movement method is in First Person. I made a short video to show what I mean:
    The first part of the video the movement method is in First Person and I'm able to look around using the cursor locked to the center of the screen. The last part of the video I changed the settings to Point and Click movement so the pathfinding using a navmesh would work (sorry if the last part is hard to see, but take notice of the circle on the floor (click marker) before the movement starts).

    The Character: Move to point action (and what you have in the Movement template) gets close to what I am looking for, but I'd rather not have to set predefined points using markers, but let the player be able to move to whatever point they click on.

  • Switch to First Person movement, and create a new Cutscene with a single Character: Move to point Action. Have it command the Player to move to a new Marker, and check Wait until finish?.

    Place the following script on the Marker:

    using UnityEngine;
    using AC;
    
    public class FirstPersonPointClick : MonoBehaviour
    {
    
        public ActionList onClick;
    
        void Update ()
        {
            if (KickStarter.stateHandler.IsInGameplay () && Input.GetMouseButtonDown (0))
            {
                Ray ray = KickStarter.CameraMain.ScreenPointToRay (Input.mousePosition);
                RaycastHit hit = new RaycastHit();
    
                if (KickStarter.settingsManager && KickStarter.sceneSettings && Physics.Raycast (ray, out hit, KickStarter.settingsManager.navMeshRaycastLength))
                {
                    transform.position = hit.point;
                    onClick.Interact ();
                }
            }
        }
    
    }
    

    Assign the Cutscene in the Inspector's "On Click" field, and run the scene. This will cause the Marker to teleport when the Player clicks on the NavMesh, and then run the Cutscene.

  • Thanks, Chris! The script works. Only thing is that I can no longer click on hotspots in my scene as it seems all clicks are stolen by the new move script. I'm also noticing that if I click on walls in my scene it will try to move my player into the wall and get stuck. I know I can use the "Enforce time limit?" option to get unstuck, but it would be cool if the clicks only responded to clicking on the navmesh so the player didn't have to wait for the time limit to run out before they can move again. Please let me know if this is possible. Thanks!

  • The script should only have an effect when explicitly clicking on collider objects that are placed on the NavMesh layer. Is this true of the walls in your scene?

  • edited January 2020

    The colliders for my walls are on the Default layer, but if I change that to the Ignore Raycast layer then I get the desired behavior, clicks respond only clicking on the floor (navmesh).

    However, I do need some colliders to block raycasts by setting them to the Default layer so the player can't click through walls. Is there a way to have these raycasts ignore the Default layer?

    I think this is also why its stealing my clicks when clicking on hotspots since those are on the Default layer too.

  • edited January 27

    Try this:

    using UnityEngine;
    using AC;
    
    public class FirstPersonPointClick : MonoBehaviour
    {
    
        public ActionList onClick;
    
        void Update ()
        {
            if (KickStarter.stateHandler.IsInGameplay () && Input.GetMouseButtonDown (0))
            {
                Ray ray = KickStarter.CameraMain.ScreenPointToRay (Input.mousePosition);
                RaycastHit hit = new RaycastHit();
    
                LayerMask layerMask = 1 << LayerMask.NameToLayer (KickStarter.settingsManager.navMeshLayer);
    
                if (KickStarter.settingsManager && KickStarter.sceneSettings && Physics.Raycast (ray, out hit, KickStarter.settingsManager.navMeshRaycastLength, layerMask))
                {
                    transform.position = hit.point;
                    onClick.Interact ();
                }
            }
        }
    
    }
    
  • That works! Thanks, Chris!

  • Hi @ChrisIceBox I'm trying to use this script in AC 1.79.3 but probably I am doing it wrong. I want to make a first person point and click. I want a game just exactly like a standard 3d point and click, with hotspots, pathfinding, etc but in first person. I started a 3d point and click project, then I switch to First Person movement method and configure the imput manager for XY axis (following your first person tutorial on youtube).

    Then I made a new cut scene like you explain on this thred. I made a new green marker in the secene and placed the FirstPersonPointClick Script and asign the cutscene on it.

    Am I doing something wrong?

    This is the second game that I'm doing whith AC, I love it but i need some help :(

    Sorry for my English...

  • The steps you've taken sound right, but I'm not sure of the problem. What is the exact issue you're facing?

  • edited January 26

    The player is moving like a normal (wasd/mouse) but when I try to click on floor or walls it doesn't move. I want that the player only move when I click a point or when I click a hotspot with "walk to marker" configured. I want to disable the standard "wsad" control.

    I make this video capture with my project trying to show every relevant thing about the problem https://youtu.be/g9o6M_8xSJo

    I also have some player animator alerts in the console but before change the player to a first person one the point & click worked well without animator.

    I also have this error in the console:

    Resolve of invalid GC handle. The handle is from a previous domain. The resolve operation is skipped.
    UnityEngine.GUILayout:Window (int,UnityEngine.Rect,UnityEngine.GUI/WindowFunction,string,UnityEngine.GUIStyle,UnityEngine.GUILayoutOption[])
    AC.ActionListEditorWindow:NodesGUI (bool,UnityEngine.Event) (at Assets/AdventureCreator/Scripts/ActionList/Editor/ActionListEditorWindow.cs:1795)
    AC.ActionListEditorWindow:OnGUI () (at Assets/AdventureCreator/Scripts/ActionList/Editor/ActionListEditorWindow.cs:254)
    UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

    If you need more information just ask for it, I give it to you as fast as I can.

    Thanks for all and sorry if I'm making some dumb mistake :(

  • The Action has no character assigned - check Is Player?.

  • Perfect! it was that. I knew it had to be some small thing that I had forgotten. I'm going to continue testing to try to get the exact gamefeel I'm looking for.

    I'm sure some other problem will arise along the way. Sorry for trying to take your product beyond what it is intended for. The rest of the features are so solid and easy to use that I think it's worth the effort.

  • Ok, now a couple of strange things happen to me with hotspots and walls.

    I have captured another video to try to see the problem: https://youtu.be/V1NNqI9tB_0

    -On the one hand you can see that there is a ramp behind it. The idea was not to be able to click to go directly to the ramp, but it seems that the click goes through the wall and makes the player go directly to a place that he is not seeing.
    -On the other hand, when we get to the top, we see how the player only goes to the light hotspot marker when we click and there is no element behind it. If we click on the hotspot but there is ground behind it, the player goes to the ground, completely ignoring the hotspot. This is a problem especially if there is a wall behind it, since it will always ignore the hostpot.

    Finally, I would also like the camera to be able to reset itself by facing forward when clicking on a hotspot, since if the hostpot is high the camera keeps looking up throughout the entire journey until it takes control again.

    I'm sorry if I ask too much, I know this is not your job, but I am sure that my project will be viable if I can solve these last control issues.

  • edited January 27

    Here's an update scripw with two changes:

    1, Clicks are now ignored when a Hotspot is selected.
    2, The LayerMask is now configurable - if the wall beside the ramp is included, clicks will not pass through it to the ramp.

    using UnityEngine;
    using AC;
    
    public class FirstPersonPointClick : MonoBehaviour
    {
    
        public ActionList onClick;
        public LayerMask layerMask;
    
        void Update ()
        {
            if (KickStarter.stateHandler.IsInGameplay () && Input.GetMouseButtonDown (0) && KickStarter.playerInteraction.GetActiveHotspot () == null)
            {
                Ray ray = KickStarter.CameraMain.ScreenPointToRay (Input.mousePosition);
                RaycastHit hit = new RaycastHit();
    
                if (KickStarter.settingsManager && KickStarter.sceneSettings && Physics.Raycast (ray, out hit, KickStarter.settingsManager.navMeshRaycastLength, layerMask))
                {
                    transform.position = hit.point;
                    onClick.Interact ();
                }
            }
        }
    
    }
    
  • Perfect Chris, now it works perfect.

    Now I'm trying the different ways to move to a hotspot.

    I am interested in being able to reach a hotspot from any direction and for the player to choose the easiest path, however (as seen in the video), the player seems to have a preference for a specific side of the hotspot regardless of the starting point. I don't really understand why he does that.

    I have tried rotating the hotspot and it does the same. I don't know where the reference to the front point of the hotspot comes from to always want to go to the same point instead of staying behind when you go to it from behind.

    You can see the problem in this video. https://youtube.com/watch?v=92_1_iExr24

    I have continued writing in this thread because I think it may be related to having adapted point and click to first person. If you think it should be in a new thread, sorry for the inconvenience.

  • When the Player Action field is set to Walk To, they'll move towards the centre of the Hotspot. In terms of pathfinding, they'll move to the point on the NavMesh that's closest to the Hotpot's Transform position.

    This may be why rotating the Hotspot makes no difference - if the position remains the same, so does the closest point on the NavMesh.

    This raises a good point, though. I shall look into the possibility of AC instead moving the Player to the closest bound on the Hotspot's bounds.

    For now, the way around this would be to assign a Walk-to Marker to the Hotspot, set the Player action to Walk To Marker, and then attach a short script to the Marker that repositions itself to always be closer to the Player than the Hotspot itself:

    using UnityEngine;
    using AC;
    
    public class DynamicMarker : MonoBehaviour
    {
    
        public Hotspot hotspot;
        public float distance = 0.3f;
    
        void Update ()
        {
            Vector3 direction = KickStarter.player.transform.position - hotspot.transform.position;
            direction.y = 0f;
            direction.Normalize ();
            transform.position = hotspot.transform.position + direction * distance;
        }
    
    }
    
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.