![](/img/trans.png)
[英]I am trying to insert records into a an object list but nothing gets inserted when I call the method. What am I doing wrong?
[英]Object gets stuck when trying to bounce off screen edges, am I doing this wrong?
public class AsteroidMovement : MonoBehaviour
{
public Vector2 speed;
public Vector2 direction;
private Vector2 movement;
private Vector3 TopScreenBound;
private Vector3 BottomScreenBound;
// Use this for initialization
void Start()
{
TopScreenBound = Camera.main.ViewportToWorldPoint(new Vector3(0f, 1f, 0f));
BottomScreenBound = Camera.main.ViewportToWorldPoint(new Vector3(0f, 0f, 0f));
}
// Update is called once per frame
void Update()
{
if (gameObject.transform.position.y >= TopScreenBound.y)
{
direction.y *= -1;
}
if (gameObject.transform.position.y <= BottomScreenBound.y)
{
direction.y *= -1;
}
movement = new Vector2(speed.x * direction.x, speed.y * direction.y);
}
void FixedUpdate()
{
rigidbody2D.velocity = movement;
}
}
我正在嘗試讓游戲中的小行星從屏幕邊緣彈起,並且使它運行正常,但是經過幾次彈跳,小行星/物體會“卡在”牆壁上並出現故障。
我要解決這個錯誤嗎? 我看不到代碼在幾次反彈后卡在小行星上的原因,謝謝:)
您必須固定對象的位置才能在屏幕內彈起,如果您的對象已經在屏幕外並且在下一幀中沒有完全進入屏幕空間,那么您的對象將無限地改變其方向,直到它進入或離開屏幕。
更改此:
if (gameObject.transform.position.y >= TopScreenBound.y)
{
direction.y *= -1;
}
if (gameObject.transform.position.y <= BottomScreenBound.y)
{
direction.y *= -1;
}
對此:
if (gameObject.transform.position.y >= TopScreenBound.y)
{
gameObject.transform.position.y = TopScreenBound.y;
direction.y *= -1;
}
if (gameObject.transform.position.y <= BottomScreenBound.y)
{
gameObject.transform.position.y = BottomScreenBound.y;
direction.y *= -1;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.