Page 1 of 1

Save System With Kinematic Controllers

Posted: Thu Dec 28, 2023 7:44 pm
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

Re: Save System With Kinematic Controllers

Posted: Thu Dec 28, 2023 8:44 pm
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.

Re: Save System With Kinematic Controllers

Posted: Thu Dec 28, 2023 10:53 pm
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

Re: Save System With Kinematic Controllers

Posted: Fri Dec 29, 2023 9:03 am
by Tony Li
Thanks for sharing!