我的玩家角色在加载保存位置时掉下盒子
My player character on load saved position falls of box
你可以在 youtube 视频上看到,我的角色加载后掉了下来。我想知道为什么? https://youtu.be/_CKNaYBxvhQ?t=1
当我保存播放器位置时,加载位置相同。跌倒发生在加载位置后。
这是整个项目,谢谢下载和回复
whole project
这个问题出现在第一张地图(玩家掉下平台)。所以我用一个平台复制了简单的地图,我使用最少的代码来移动播放器和加载保存,但问题被复制了。掉落仅来自不导致导航网格路径下降的平台。
我尝试添加墙来阻止玩家从盒子里掉下来,但玩家穿过了这堵墙。
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
在按钮菜单上,玩家的位置被保存,新场景 Canvas 和按钮显示。在该按钮上显示了先前的场景。 OnEnable 加载播放器位置。
我需要玩家正确加载并留在保存的位置。
请尝试帮助我实现制作游戏的梦想。不懂就问。
在Load();
之后添加agent.Warp(transform.position);
由于您正在重新加载场景,NavMeshAgent 具有玩家认为应该在的场景起始位置。
因此,当您加载代理的位置时(顺便说一句,也考虑保存它的旋转),NavMeshAgent 发现它应该在与实际不同的地方并且它会尽力妥协,这会导致与加载位置不同的位置。
所以,为了避免这种情况,您需要告诉 NavMeshAgent 改变它应该在的位置的状态,这可以通过 Warp(Vector3 position)
方法来完成:
void OnEnable()
{
Load();
agent.Warp(transform.position);
}
你可以在 youtube 视频上看到,我的角色加载后掉了下来。我想知道为什么? https://youtu.be/_CKNaYBxvhQ?t=1 当我保存播放器位置时,加载位置相同。跌倒发生在加载位置后。
这是整个项目,谢谢下载和回复 whole project
这个问题出现在第一张地图(玩家掉下平台)。所以我用一个平台复制了简单的地图,我使用最少的代码来移动播放器和加载保存,但问题被复制了。掉落仅来自不导致导航网格路径下降的平台。
我尝试添加墙来阻止玩家从盒子里掉下来,但玩家穿过了这堵墙。
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
在按钮菜单上,玩家的位置被保存,新场景 Canvas 和按钮显示。在该按钮上显示了先前的场景。 OnEnable 加载播放器位置。
我需要玩家正确加载并留在保存的位置。 请尝试帮助我实现制作游戏的梦想。不懂就问。
在Load();
之后添加agent.Warp(transform.position);
由于您正在重新加载场景,NavMeshAgent 具有玩家认为应该在的场景起始位置。
因此,当您加载代理的位置时(顺便说一句,也考虑保存它的旋转),NavMeshAgent 发现它应该在与实际不同的地方并且它会尽力妥协,这会导致与加载位置不同的位置。
所以,为了避免这种情况,您需要告诉 NavMeshAgent 改变它应该在的位置的状态,这可以通过 Warp(Vector3 position)
方法来完成:
void OnEnable()
{
Load();
agent.Warp(transform.position);
}