简体   繁体   English

如何使这个检查点系统在 unity3d 中工作?

[英]How do I make this checkpoint system work in unity3d?

I want to have the player start from a start position at first.我想让播放器首先从 position 开始。 Then start from a checkpoint after they've completed level one.然后在他们完成第一级后从检查点开始。 I've tried different iterations of this script, it is supposed to use a bool check if the player has passed through a trigger, and if they have their position will be equal to the checkpoint at Start.我已经尝试过这个脚本的不同迭代,它应该使用布尔检查玩家是否通过了触发器,如果他们有他们的 position 将等于开始时的检查点。 It has to remember the bool when the game is turned off and on again.当游戏关闭并再次打开时,它必须记住布尔值。

Right now the player always starts from the checkpoint.现在玩家总是从检查点开始。

I tried making my own boolean with playprefs but ended up using BoolPrefs, which still didn't work.我尝试使用 playprefs 制作自己的 boolean,但最终使用了 BoolPrefs,但仍然无法正常工作。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public Rigidbody player;
    public Transform startPoint;
    public Transform checkPoint;

    private void Start()
    {
        if (PlayerPrefsX.GetBool("level01Complete", false))
        {
            player.transform.position = startPoint.position;
        }

        if (PlayerPrefsX.GetBool("level01Complete", true))
        {
            player.transform.position = checkPoint.position;
        }
    }



    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "HubTrigger")
        {
            PlayerPrefsX.SetBool("level01Complete", true);
        }
        else
        {
            PlayerPrefsX.SetBool("level01Complete", false);
        }
    }

}

Here is the BoolPrefs script I'm using currently.这是我目前使用的 BoolPrefs 脚本。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerPrefsX 
{


        public static void SetBool(string name, bool booleanValue)
        {
            PlayerPrefs.SetInt(name, booleanValue ? 1 : 0);
        }

        public static bool GetBool(string name)
        {
            return PlayerPrefs.GetInt(name) == 1 ? true : false;
        }

        public static bool GetBool(string name, bool defaultValue)
        {
            if (PlayerPrefs.HasKey(name))
            {
                return GetBool(name);
            }

            return defaultValue;
        }

}

I am new to using PlayerPrefs so there may just be something obvious that I'm overlooking.我是使用 PlayerPrefs 的新手,所以我可能忽略了一些明显的东西。

HubTrigger is the trigger you pass through after completing level one. HubTrigger 是您完成第一关后通过的触发器。 So it should be that the first time you play you start from one spot, then once you pass through hubtrigger, you've completed level one and from then on you start from the checkpoint when you load the game.所以应该是你第一次玩的时候从一个地方开始,然后一旦你通过hubtrigger,你就完成了第一关,然后你在加载游戏时从检查点开始。

PlayerPrefsX looks fine. PlayerPrefsX看起来不错。 It just seems like you're using it incorrectly.似乎您使用不正确。 You should check if (PlayerPrefsX.GetBool("level01Complete", false)) , and if that's true, then set the checkpoint position, and set the start position otherwise:您应该检查if (PlayerPrefsX.GetBool("level01Complete", false)) ,如果为真,则设置检查点 position,否则设置开始 position:

private void Start()
{
    if (PlayerPrefsX.GetBool("level01Complete", false))
    {
        player.transform.position = checkPoint.position;
    }
    else 
    {
        player.transform.position = startPoint.position;
    }
}

For OnTriggerEnter , you really only should set level01Complete there in the event you beat level 1:对于OnTriggerEnter ,您真的应该只在您击败级别 1 时设置level01Complete

private void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "HubTrigger")
    {
        PlayerPrefsX.SetBool("level01Complete", true);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM