Save System With Kinematic Controllers

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
c166
Posts: 38
Joined: Fri Oct 14, 2022 11:08 pm

Save System With Kinematic Controllers

Post by c166 »

Hey Tony,

So changed the character controller to be a kinematic character controller, and it seems to be conflicting with PositionSaver (i.e. the controller won't let the position be set via the default SetPosition).

I've had a playaround and think I've got a solution but I can't get it to actually work since the PositionSaver script doesn't recognize external namespaces for some reason. Theoretically the below SHOULD work if the PixelCrusher namespace was able to use the KinematicCharacterController namespace (replace the PixelCrusher code setting the navmesh agent/ transform).

Code: Select all

        protected virtual void SetPosition(Vector3 position, Quaternion rotation)
        {
            player.GetComponent<KinematicCharacterMotor>().SetPositionAndRotation(position, rotation);
        }
I'm not sure why external namespaces (I've tried other namespaces as well) aren't recognized in the PixelCrushers' saver systems (I assume as a safety thing, but I don't really know how that's working). Other scripts are able to pick up and use external namespaces, but let me know if you think it's a local issue.

I THINK that the above would be the "cleanest" solution (assuming it works) since it'd retain the per-scene saving of location etc.

I'm not sure if that's the best way to resolve the problem though; so alternatively, if you could provide boilerplate code to extract the position of an object based on the key -> I could try figure out how to call the Kinematic Set Position from outside the Saver code location as well.

Best Regards,
c
Attachments
namespace.PNG
namespace.PNG (1.82 KiB) Viewed 829 times
NameSpaceNotLoaded.PNG
NameSpaceNotLoaded.PNG (13.09 KiB) Viewed 829 times
position_saver_key.PNG
position_saver_key.PNG (14.41 KiB) Viewed 829 times
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save System With Kinematic Controllers

Post by Tony Li »

Hi,

PositionSaver is in the Plugins folder. Scripts in the Plugins folder can't access scripts outside of Plugins.

That doesn't mean you should move PositionSaver, though! Instead, create a subclass of PositionSaver in your own scripts folder, and override the SetPosition method:

Code: Select all

public class MyKinematicPositionSaver : PositionSaver
{
    protected override void SetPosition(Vector3 position, Quaternion rotation)
    {
        GetComponent<KinematicCharacterMotor>().SetPositionAndRotation(position, rotation);
    }
}
Then use this subclass on your player instead of PositionSaver.
c166
Posts: 38
Joined: Fri Oct 14, 2022 11:08 pm

Re: Save System With Kinematic Controllers

Post by c166 »

Hey Tony,

Sweet, thanks for that - appreciate the fast reply as always :)

Good to know that scripts in the Plugins folder can't access scripts from outside it. The solution worked perfectly with the Kinematic Controller, and now I've got the expected behavior again (Character loads in the same spot).

In case other people bump into the same issue:

Code: Select all

public class PositionSaverUpdate : PositionSaver
{
    GameObject player;
    KinematicCharacterMotor motor;
    public override void Start()
    {
        base.Start();
        GameObject player = GameObject.FindGameObjectWithTag("Player");
        motor = player.GetComponent<KinematicCharacterMotor>();
    }
    protected override void SetPosition(Vector3 position, Quaternion rotation)
    {
        motor.SetPositionAndRotation(position, rotation);
    }
}
Best Regards,
C
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save System With Kinematic Controllers

Post by Tony Li »

Thanks for sharing!
Post Reply