[解决]
我一直在跟随Udemy的“通过制作游戏学习编码”课程(Block Breaker Section),我有一个小小的问题。恼人的问题。我的桨没有正确地夹住我输入的值。我本来没有遇到任何麻烦,但我决定在左侧有一个带有速度控制器,声音控制器和生命的小菜单。这意味着我只能说我希望拨片停在屏幕边缘。
观察到的行为:
划水板不会将自身限制在我放入的区域。随着屏幕尺寸的改变,划水板将离开屏幕或在屏幕中间某处停止。
我想要它做什么:
我希望当它碰到左边菜单的边缘时停止,当它击中右边的屏幕时停止。我也希望这与我的屏幕尺寸变化保持一致。所以基本上我需要一个可以确定这些点的数学方程式,它需要能够根据屏幕尺寸进行调整。
以下是我的划桨代码:
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;
}
}
答案 0 :(得分:0)
这是因为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
答案 1 :(得分:0)
尝试将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);
}
}
答案 2 :(得分:0)
尝试这样的事情:(伪代码,未经测试,给你一个想法)
请检查每个步骤的评论。
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;
}