Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用数学方程来阻止对象离开屏幕?_C#_Unity3d - Fatal编程技术网

C# 如何使用数学方程来阻止对象离开屏幕?

C# 如何使用数学方程来阻止对象离开屏幕?,c#,unity3d,C#,Unity3d,[已解决] 我一直在遵循Udemy的“通过制作游戏课程块断路器来学习代码”部分,我有一个恼人的问题。我的球拍没有按我输入的值正确夹紧。我不会有任何问题,但我决定,我想要一个小菜单的左侧与速度控制器,声音控制器和生活。这意味着我不能说我想让划桨停在屏幕边缘 ​观察到的行为: ​桨叶不会将自身约束在我放置的区域。当屏幕大小改变时,挡板将离开屏幕或停在屏幕中间的某个位置 ​我希望它做什么: ​我想当它碰到我菜单左边的边缘时停止,当它碰到右边的屏幕时停止。我也希望在屏幕大小改变时保持不变。所以基本上我需

[已解决]

我一直在遵循Udemy的“通过制作游戏课程块断路器来学习代码”部分,我有一个恼人的问题。我的球拍没有按我输入的值正确夹紧。我不会有任何问题,但我决定,我想要一个小菜单的左侧与速度控制器,声音控制器和生活。这意味着我不能说我想让划桨停在屏幕边缘

​观察到的行为:

​桨叶不会将自身约束在我放置的区域。当屏幕大小改变时,挡板将离开屏幕或停在屏幕中间的某个位置

​我希望它做什么:

​我想当它碰到我菜单左边的边缘时停止,当它碰到右边的屏幕时停止。我也希望在屏幕大小改变时保持不变。所以基本上我需要一个数学方程来确定这些点,它需要能够根据屏幕大小进行调整

这是我的划桨代码:​


这是因为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;
}

行为取决于您如何计算//左值和//右值,如果没有这些信息,就无法回答。另外,我也不会这么快说Mathf.Clamp不起作用,而是重新检查了传递的参数。很好,我更改了标题。还有什么办法吗?@MatthewInglis如果你找到了答案,请点击答案旁边的灰色复选标记接受。否则,请发表你自己的文章,向别人展示你是如何解决这个问题的。非常感谢,但是我应该为这个数学夹具投入什么价值呢?我尝试了一个固定的值,一个百分比,屏幕。宽度…但都没有成功。Mathf.Clamp为值设置边界。这取决于你想把桨放在哪里。例如,您正在使用16个单位x和10个单位y的世界空间。摄像机的位置是0,0,-10,你们有一个宽度为2个单位的挡板。所以你们的leftValue应该是世界空间左边缘的-8,而rightValue应该是左边缘的-6+桨叶的宽度。当然,这些是左边的桨的值。好的,我已经按照你说的做了。这是我得到的:世界空间是1920 x 1080 y,桨的宽度是200。我将相机设置为你在示例中所说的。所以我的左值是-960,右值是-760。我已经把它们放进去了,但它们不起作用……不,1920x和1080y是你的屏幕空间。查看场景中的栅格,每个栅格在世界空间中为1个单位。为了更容易地获得世界空间y,请在inspector查看主摄像头的大小。它可能是'5',这意味着你有10个单元,5在x轴下,5在y轴上x轴上。所以在x上大约有17.7个单位。你应该看看你的桨有多少格宽。如果它是雪碧,宽度为200,那么它在世界空间中应该是2。因此,对于编辑,左边的值应该是-8.817.7/-2,右边的值应该是-6.8-8.8+2。尽管我不喜欢它,但它给了我一大堆错误。这是monodevelop的错误截图抱歉,更新中有一个额外的右括号。现在试试?@MatthewInglis好极了,如果你觉得我回答了你的问题,请把这个标记为答案。。。如果我是有帮助的,请投票,这样其他人就会知道这是有帮助的。
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;
}