【发布时间】:2018-08-23 05:52:19
【问题描述】:
我正在阅读教程,并且我了解了大部分内容。我想问一件事。 这是我正在学习的教程:
https://noobtuts.com/unity/2d-pong-game
这是被称为函数HitFactor的方法。
if (col.gameObject.name == "RacketLeft") {
// Calculate hit Factor
float y = hitFactor(transform.position, col.transform.position, col.collider.bounds.size.y);
// Calculate direction, make length=1 via .normalized
Vector2 dir = new Vector2(1, y).normalized;
// Set Velocity with dir * speed
GetComponent<Rigidbody2D>().velocity = dir * speed;
}
Hit Factor 方法是
float hitFactor(Vector2 ballPos, Vector2 racketPos,
float racketHeight) {
// ascii art:
// || 1 <- at the top of the racket
// ||
// || 0 <- at the middle of the racket
// ||
// || -1 <- at the bottom of the racket
return (ballPos.y - racketPos.y) / racketHeight;
}
谁能举个例子给我解释一下?
(ballPos.y - racketPos.y) / racketHeight;
我真的无法理解,也不知道我应该读什么才能让自己理解这一点。
【问题讨论】:
-
好吧,举个例子。假设球拍长 100 像素,球在 y 位置 220,球拍中间在 y 位置 200。球作为球拍长度的一部分在哪里?在评论中?
-
非常感谢埃里克。 col.transform.position 将给出球拍的中间(y)位置?
标签: c# unity3d game-physics pong