#Having a character follow the player

1 messages ยท Page 1 of 1 (latest)

main obsidian
#

I'm trying to see if I can make a character follow the player!

AFAIK, the only game available where this is done is in Intergalactic Wizard Force. So I'm digging through the code to see if I can understand it. ( I reckon this question is mostly for @tulip estuary ๐Ÿ™‚ )

I'm trying to understand the logic behind this part of the code:

{
    // Check camera target pos is within camera
    // Camera to position
    float toPlayer = character.TargetPosition.x - Camera.GetPosition().x;
    float cameraHalfWidth = E.GetCameraGui().orthographicSize * E.GetCameraGui().aspect;
    if ( Mathf.Abs(toPlayer) > cameraHalfWidth - 30 )
    {                                    
        // walk on-screen. Maybe to pre-defined point?
        Vector2 targetPos = new Vector2(Camera.GetPosition().x+Mathf.Sign(toPlayer)*(cameraHalfWidth - 60), character.TargetPosition.y+10);
        character.WalkToBG( targetPos );
    }

} ```
I assume this is the general follow code. Why does it use the camera position? ๐Ÿ™‚
rough laurel
#

This code seems to just make sure the following character stays on screen, not right next to the player-controlled character. The first branch of this if also instructs the following character to stay out of the way as the player walks around.

#

(I remember also studying IWF source ๐Ÿ™‚ )

tulip estuary
#

Yeah exactly, I just wanted the other characters to stay on screen in that game. Just more natural than following their exact path

tulip estuary
#

For a follow more like AGS, you could-

- every few seconds (using  Utils.GetTimeElapsed or E.Timer functions)
   - check distance to player, and if outside a set distance,  WalkToBg(C.Plr)
- always check if inside the same distance (it a slightly smaller distance), and  StopWalking again
- also some code to move to the room the player is in if not in it. maybe cache the position where the player entered so they can appear there```
main obsidian
main obsidian
main obsidian
#

Got some basic follow code working really well now. Thanks for all the help!

#

One minor thing though, there is a part in the original code from Wizard Force that I think checks if the target position is inside the walkable area. It starts with something like this:

targetPos = R.Current.Instance.GetComponent<RoomComponent>().GetPathfinder().GetClosestPointToArea(targetPos)

If I have this code enabled, I found the character would sometimes run off into the distance! If I disable it, everything appears to work better. I wonder what could be causing the issue?

#

Is the "GetClosestPointToArea" a piece of Unity or a piece of PowerQuest?

main obsidian
#

Also! Can I get the value of a scale region at a characters position?

rough laurel
rough laurel
main obsidian
main obsidian
#

NOOB QUESTION WARNING!!

I don't know much about C#, but I hope it's OK I ask here instead of StackOverflow (where I'll probably get roasted for not phrasing the question correctly!)

I get a big fat red error if there are no regions named the exact name in the scene. Is there a way to check if the named region exists before trying to access it? I tried this:

if (R.Current.GetRegion("TestScale") != null ) { ..code.. }

But that also returns a red warning sign.

prime osprey
#

You can use a try/catch to catch the error: https://www.w3schools.com/cs/cs_exceptions.php

#

What it does is basically try to execute what's inside the try statement. if there's no error then it will do it "for real" and if there's one it will execute what's in the catch part instead

rough laurel
# main obsidian NOOB QUESTION WARNING!! I don't know much about C#, but I hope it's OK I ask he...

By the way, a nice little life hack these days is to ask ChatGPT. It's not super-reliable with advanced stuff, but it's almost 100% accurate with very simple questions like this, so you can get help and avoid roasting by arrogant serious looking guys ๐Ÿ˜„ I do that a lot lately, I almost forgot how to google.

Not that you're not welcome to ask here anyway of course, everyone is eager to help no matter how nooby a question is ๐Ÿ˜‰

prime osprey
#

I can't write code properly on the mobile, but the W3 link has a few examples

main obsidian
#

Thanks again, the both of you! This is really, really helpful!

tulip estuary
#

Can't remember if that error is an exception, so try/catch might not work...

But you can iterate through the list manually to find the one you want if you want to hack something in.

#

Most of the PowerQuest classes have the public interface you get with R.Forest. (That returns an IRoom) but for advanced hackery, you can cast to Room to do stuff with the juicy internals

#

if GetScaleAt is a useful function I can expose that in future, seems like it would be for various things.

#

It's pretty easy to expose in PowerquestExtentions though I guess

#

If it's just a one off you need it for, this should do it. Otherwise, adding a couple of functions to PowerQuestExtentions would make life easier if you want to be doing it a lot

        float scale = 1;
        Region myRegion = (R.Current as Room).GetRegions().Find(item=> item.ScriptName.EqualsIgnoreCase("TestRegion") );
        if ( myRegion != null )
            scale = myRegion.GetInstance().GetScaleAt(position);
#

About the pathfinding issue- It could be due to having regions blocking off areas of the map? Wizard Force was pretty hacky for a jam so wouldn't have been tested in many situations.

I think character avoidance was added since that game too, so possibly that's breaking it... You could try setting the C.myCharacter.Solid to false before calling GetClosestPointToArea and then reset it after...

Also, GetClosestPointToArea check is only needed if the character's not in a walkable area, so could check if (PowerQuest.Get.Pathfinder.IsPointInArea( characterPosition ) == false ) first?

main obsidian
#

Thanks for the code, Dave! I'll work from that and see if I can write something that iterates through the regions, finding the regions that set the scale.