简体   繁体   English

如何在Unity中显示player.transform.position?

[英]How to display player.transform.position in Unity?

In my game on Unity3d I work on save/load game and I want to display player.transform.position , but it always shows 0, 0, 0 . 在Unity3d上的游戏中,我从事保存/加载游戏的工作,我想显示player.transform.position ,但它始终显示0, 0, 0 Can't understand why 不明白为什么

在此处输入图片说明

public GameObject player;    
private Transform playerTransform; 
//..
playerTransform = player.GetComponent<Transform>();

Save Data 保存数据

PlayerPrefs.SetFloat("x" + currentActiveSlot, playerTransform.position.x);
PlayerPrefs.SetFloat("y" + currentActiveSlot, playerTransform.position.y);
PlayerPrefs.SetFloat("z" + currentActiveSlot, playerTransform.position.z);
PlayerPrefs.Save();

Load Data 载入资料

Debug.Log("transform before: " + transform.position); // all time 0,0,0
playerTransform.position = new Vector3(PlayerPrefs.GetFloat("x" + currentActiveSlot),
                                       PlayerPrefs.GetFloat("y" + currentActiveSlot),
                                       PlayerPrefs.GetFloat("z" + currentActiveSlot));        
Debug.Log("transform after: " + transform.position); // all time 0,0,0

You are displaying transform.position which is not set anywhere. 您正在显示未在任何地方设置的transform.position。

Load Data 载入资料

Debug.Log("transform before: " + transform.position); // all time 0,0,0
transform.position = new Vector3(PlayerPrefs.GetFloat("x" + currentActiveSlot),
                                       PlayerPrefs.GetFloat("y" + currentActiveSlot),
                                       PlayerPrefs.GetFloat("z" + currentActiveSlot));        
Debug.Log("transform after: " + transform.position); // all time 0,0,0

or 要么

Debug.Log("transform before: " + playerTransform.position); // all time 0,0,0
playerTransform.position = new Vector3(PlayerPrefs.GetFloat("x" + currentActiveSlot),
                                       PlayerPrefs.GetFloat("y" + currentActiveSlot),
                                       PlayerPrefs.GetFloat("z" + currentActiveSlot));        
Debug.Log("transform after: " + playerTransform.position); // all time 0,0,0

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

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