【发布时间】: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); -
看起来有效。非常感谢。