摘要:我正在嘗試統一的東西以更好地理解它。我創建了一個帶有剛體的 3d 物件并凍結了 y 軸。我放置在相機上以向下看 3d 物件,該物件可以使用 x 和 z 軸向左、向右和向上、向下移動。
問題:問題是玩家正在走出螢屏的邊界。
這是我的代碼,我根據自己的理解(在統一手冊中)和其他人有同樣問題的代碼。
private Vector3 transformPos;
void LateUpdate ()
{
//Defined the boundaries of the screen by using ViewPortToWorldPoint. I tried ViewportToWorldPoint as well and it did not work
float left = Camera.main.WorldToViewportPoint(Vector3.zero).x;
float right = Camera.main.WorldToViewportPoint(Vector3.one).x;
float top = Camera.main.WorldToViewportPoint(Vector3.zero).y;
float bottom = Camera.main.WorldToViewportPoint(Vector3.one).y;
//Brought the player transform from World units to screen unit. Where x is x, z from the 3d object acts as y, and y is z.
transformPos = Camera.main.WorldToViewportPoint (transform.position);
//This debug was to check if z is y in the ViewportPoint and it was
Debug.Log(transformPos);
float x = transformPos.x;
//Calling it z because I dont wanna confuse myself when I convert it back
float z = transformPos.y;
//This is where I start the clamping using if statements
if(transformPos.x <= left)
{
x = Mathf.Clamp(transformPos.x, 0.1f,0.9f);
}
else if(transformPos.x >= right)
{
x = Mathf.Clamp(transformPos.x, 0.1f,0.9f);
}
if(transformPos.y <= top)
{
z = Mathf.Clamp(transformPos.y, 0.1f,0.9f);
}
else if( transformPos.y >= bottom)
{
z = Mathf.Clamp(transformPos.y, 0.1f,0.9f);
}
//And then finally I convert it back to world unit where the values is just placed in. x is x, y of the 3d object remains the same, and z
transform.position = Camera.main.ViewportToWorldPoint(new Vector3(x,transform.position.y,z));
}
下圖是我從這段代碼中得到的:

正如您從我的主攝像頭右下方看到的那樣,它完全超出了我的螢屏范圍,我無法移動它。所以我的代碼顯然有問題,但我不明白我錯在哪里。
編輯#1
圖片中的立方體是我用作播放器的立方體
編輯#2
下面的代碼是我附在我的播放器上的播放器移動腳本以及上面的腳本。我不認為它與它沖突,但以防萬一。移動也有一點問題(我正在嘗試使用手機傾斜功能移動播放器),但我也在嘗試修復它
Rigidbody playerRigidbody;
public float speed = 10;
private Vector3 movement;
private Vector3 tilt ;
// Start is called before the first frame update
void Start()
{
playerRigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
tilt = Input.acceleration.normalized;
tilt = Quaternion.Euler(90f, 0, 0) * tilt;
//movement = new Vector3(Input.acceleration.normalized.x, 0.0f, 0.0f);
}
void FixedUpdate()
{
// horizontal movement code through device rotation
//var movement = new Vector3(Input.acceleration.normalized.x, 0.0f, Input.acceleration.normalized.z);
//playerRigidbody.velocity = movement * speed;
MoveCharacter(tilt);
}
private void MoveCharacter(Vector3 direction)
{
//playerRigidbody.velocity = movement * speed;
playerRigidbody.MovePosition(transform.position (direction * speed * Time.deltaTime));
}
uj5u.com熱心網友回復:
private Vector3 transformPos;
void LateUpdate()
{
//Defined the boundaries of the screen by using ViewPortToWorldPoint. I tried ViewportToWorldPoint as well and it did not work
float LeftBottom = 1;
float RightBottom = 0;
float LeftTop = 1;
float RightTop = 0;
Debug.Log($"Left: {LeftBottom} \n Right: {RightBottom} \n Top: {LeftTop} \n Bottom: {RightTop}");
//Brought the player transform from World units to screen unit. Where x is x, z from the 3d object acts as y, and y is z.
transformPos = Camera.main.WorldToViewportPoint(transform.position);
//This debug was to check if z is y in the ViewportPoint and it was
Debug.Log(transformPos);
float x = transformPos.x;
//Calling it z because I dont wanna confuse myself when I convert it back
float z = transformPos.y;
//This is where I start the clamping using if statements
if (transformPos.x <= LeftBottom)
{
x = Mathf.Clamp(transformPos.x, 0.1f, 0.9f);
Debug.Log(x);
}
else if (transformPos.x >= RightBottom)
{
x = Mathf.Clamp(transformPos.x, 0.1f, 0.9f);
}
if (transformPos.y <= LeftTop)
{
z = Mathf.Clamp(transformPos.y, 0.1f, 0.9f);
}
else if (transformPos.y >= RightTop)
{
z = Mathf.Clamp(transformPos.y, 0.1f, 0.9f);
}
//And then finally I convert it back to world unit where the values is just placed in. x is x, y of the 3d object remains the same, and z
transform.position = Camera.main.ViewportToWorldPoint(new Vector3(x, z, 1));
}
uj5u.com熱心網友回復:
上面的代碼有效。謝謝@Ubaldo Vitiello。我在這里添加我自己的答案的原因也是為了解釋為什么玩家會在完全停止之前移動一點然后口吃。花了我一段時間,但我終于明白了原因。這是因為“WorldToViewportPoint”與“rigidbody.MovePosition”不兼容。現在這只是根據我的理解,所以我可能是錯的,但 MovePosition 是將玩家的剛體移動到您指定的位置的位置。該位置只出現在世界空間中。"WorldToViewportPoint" 將世界空間轉換為視口空間.這就是為什么我的播放器在完成完全停止之前移動了一點,因為我附加到播放器的第二個腳本正在將其轉換為視口空間。現在我的玩家會卡頓的原因是因為我當時正在將視口空間轉換回世界空間。這兩個代碼根本不兼容,僅此而已。我如何解決它是使用rigidbody.velocity
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/440945.html
上一篇:Input.GetKeyDown(KeyCode.Mouse0)未按預期作業
下一篇:如何平滑旋轉立方體
