簡體   English   中英

如何使用數學方程式阻止物體離開屏幕?

[英]How to use a mathematic equation to stop object going off screen?

[解決了]

我一直在關注Udemy的“通過編寫游戲學習編寫代碼”課程(“突破塊”部分),並且遇到了一個令人討厭的問題。 我的撥片不能正確固定在我輸入的值上。我對此不會有任何問題,但我決定要在左側設置一個小菜單,其中包含一個速度控制器,一個聲音控制器,以便生活。 這意味着我不能只說我希望槳板停在屏幕邊緣。

觀察到的行為:

槳不會將自己限制在我放入的區域中。隨着屏幕尺寸的變化,槳將離開屏幕或停在屏幕中間等位置。

我想要做的是:

我希望槳板在碰到左側菜單邊緣時停止轉動,而在碰到右側屏幕時停止轉動。 我也希望這與我的屏幕尺寸更改保持相同。 所以基本上我需要一個可以確定這些點的數學方程,並且需要能夠根據屏幕尺寸進行調整。

這是我的球拍代碼:

using UnityEngine;
using System.Collections;
public class Paddle : MonoBehaviour {

    public Texture texture;

    public void Update () {

        Vector3 paddlePos = new Vector3 (0.5f, this.transform.position.y , 0f);

        float mousePosInBlocks = Input.mousePosition.x;

        paddlePos.x = Mathf.Clamp(mousePosInBlocks, //Left Vaule, //Right Value);

        this.transform.position = paddlePos;
    }
}

發生這種情況是因為Input.mousePosition返回鼠標在屏幕上的位置,而不是在世界空間中。 只需將mousePosition轉換為世界位置,然后將其值分配給mousePosInBlocks。

using UnityEngine;
using System.Collections;
public class Paddle : MonoBehaviour {

    public Texture texture;

    public void Update () {

        Vector3 paddlePos = new Vector3 (0.5f, this.transform.position.y , 0f);

        float mousePosInBlocks = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;

        paddlePos.x = Mathf.Clamp(mousePosInBlocks, leftValue, rightValue);

        this.transform.position = paddlePos;
    }
}

編輯:對不起,不是WorldToScreenPoint,ScreenToWorldPoint

嘗試僅將paddlePos設置為您要創建的新矢量(在世界空間中):

public Texture texture; // if needed for something outside this code
private Vector3 paddlePos;
private float mousePosInBlocks;
private float xValue;
private float leftValue;
private float rightValue;

public void Update () {

    mousePosInBlocks = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
    //compute leftValue on this line
    //compute rightValue on this line

    xValue = Mathf.Clamp(mousePosInBlocks, leftValue, rightValue);

    paddlePos = new Vector3 (xValue, transform.position.y , 0f);

    transform.position = paddlePos;
}

編輯基於更新的問題標題,以下應工作:

public Texture texture; // if needed for something outside this code
private Vector3 playerPosOnScreen;
private float mousePosInBlocks;

public void Update () {

    playerPosOnScreen = Camera.main.WorldToScreenPoint(transform.position);

    if (playerPosOnScreen.x > Screen.Width) {

        transform.position = Camera.main.ScreenToWorldPoint (new Vector3 (Screen.Width, transform.position.y, transform.position.z);

    } else if (playerPosOnScreen.x < 0f) {

        transform.position = Camera.main.ScreenToWorldPoint (new Vector3 (0f, transform.position.y, transform.position.z);

    }
}

嘗試這樣的事情:(偽代碼,未經測試,可以給您一個想法)

請檢查每個步驟的注釋。

public void Update () {

        // left bound
        Vector3 left = Camera.main.ViewPortToScreenPoint(new Vector3(0f, 0f, 0f));

        // Menu width
        left.x += fmyMenuWidth; // Width of menu

        // Right bound
        Vector3 right= Camera.main.ViewPortToScreenPoint(new Vector3(1f, 1f, 0f));

        // Temporary position vector    
        Vector3 paddlePos = Vector3.zero;

        // capture mouse x
        float mouseX = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;

        // capture mouse y
        float mouseY= Camera.main.ScreenToWorldPoint(Input.mousePosition).y;

        // Combine and limit the position's X-value, should not go off screen menu's width---->screen's width
        paddlePos.x = Mathf.Max(Mathf.Min(right.x, mouseX), left.x);

        // Combine and limit the position's Y-value, should not go off screen 0---->screen's height
        paddlePos.y = Mathf.Max(Mathf.Min(right.y, mouseY), left.y);

        // Finally set the bounded position
        this.transform.position = paddlePos;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM