如何使用数学方程式来阻止物体离开屏幕

本文关键字:阻止物 离开 屏幕 方程式 何使用 | 更新日期: 2023-09-27 18:10:45

[已解决]

我一直在学习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

试着将桨板Pos设置为您正在创建的新矢量(在世界空间中(:

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;
}