【问题标题】:My player character on load saved position falls of box我在加载保存位置上的玩家角色掉到了盒子里
【发布时间】:2019-07-17 18:30:44
【问题描述】:

你可以在 youtube 视频上看到,我的角色在加载后摔倒了。我想知道为什么? https://youtu.be/_CKNaYBxvhQ?t=1 当我保存播放器位置时,加载的位置是相同的。跌倒发生在加载位置之后。

这里是整个项目供下载和回复 whole project

这个问题发生在第一张地图(玩家从平台上摔下来)。所以我用一个平台复制了简单的地图,我使用最少的代码来移动播放器并用加载保存,但问题被重现了。仅从不会导致 navmesh 路径向下的平台跌落。

我尝试添加墙壁以阻止玩家从盒子中摔下来,但玩家穿过了墙壁。

using UnityEngine;
using UnityEngine.AI;
using System.Collections;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine.SceneManagement;

public class PlayerController : MonoBehaviour
{
    public Camera camera2;
    public NavMeshAgent agent;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            Ray ray = camera2.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if(Physics.Raycast(ray, out hit))
            {
                agent.SetDestination(hit.point);
            }
        }
    }
 public void Save()
 {
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");

     PlayerData data = new PlayerData();
     data.x = transform.position.x;
     data.y = transform.position.y;
     data.z = transform.position.z;

     bf.Serialize(file, data);
     file.Close();
 }

 public void Load()
 {
     //SceneManager.LoadScene("enfos");
     if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
         PlayerData data = (PlayerData)bf.Deserialize(file);
         file.Close();

         transform.position = new Vector3(data.x, data.y, data.z);
         //transform.position = tempPos;
     }
 }

 //public delegate void SerializeAction();
 //public static event SerializeAction OnLoaded; was never used can be deleted works same

 void OnEnable()
 {
     //        Debug.Log("PrintOnEnable: script was enabled");
     Load();
 }

[Serializable]
public class PlayerData
{
    public float x;
    public float y;
    public float z;
}

Player 有 Capsule Collider、Rigidbody、NavMesh Agent 和 Player Controller Script。 PlayerProperties

在按钮菜单上,玩家的位置被保存并显示带有画布和按钮的新场景。在该按钮上显示上一个场景。 OnEnable 加载玩家位置。

我需要正确加载播放器并保持在保存的位置。 请尝试帮助我做游戏是我的梦想。不懂就问吧。

【问题讨论】:

  • 如果您有时间下载整个项目并查看它。感谢您尝试帮助我。
  • 尝试在Load();之后添加agent.Warp(transform.position);
  • 看起来有效。非常感谢。

标签: c# unity3d navmesh


【解决方案1】:

Load(); 之后添加agent.Warp(transform.position);

由于您正在重新加载场景,因此 NavMeshAgent 会为玩家提供场景的起始位置,就像它认为应该在的位置一样。

因此,当您加载代理的位置时(顺便说一下,也考虑保存它的旋转),NavMeshAgent 认为它应该在某个不同的地方,它会尽力妥协,从而导致不同的放置位置而不是加载位置。

因此,为了避免这种情况,您需要告诉 NavMeshAgent 更改其应在的状态,这可以通过 Warp(Vector3 position) 方法完成:

void OnEnable()
{
    Load();
    agent.Warp(transform.position);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 2022-08-14
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多