use*_*003 2 c# unity-game-engine game-physics pong
我正在学习教程,我理解了其中的大部分内容。我想问1件事。这是我正在遵循的教程:
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;
}
Run Code Online (Sandbox Code Playgroud)
命中因子方法是
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;
}
Run Code Online (Sandbox Code Playgroud)
谁能用一个例子向我解释这一点?
(ballPos.y - racketPos.y) / racketHeight;
Run Code Online (Sandbox Code Playgroud)

我真的无法理解,也不知道我应该读什么来让自己理解这一点。
教程中描述了游戏物理
\n\n\n \n\n\n\n\n
\n- 如果球拍在顶角击中球,那么它应该弹向我们的顶部边界。
\n- 如果球拍击中球的中心,那么它应该向右反弹,而不是向上或向下。
\n- 如果球拍击中球的底部角落,那么它应该弹向我们的底部边界。
\n
这意味着方向由球拍上的击球点决定。这就是公式的内容
\n\n(ballPos.y - racketPos.y) / racketHeight\nRun Code Online (Sandbox Code Playgroud)\n\n表示。如果ballPos.y和racketPos.y相等,则球拍击中中心,球应垂直飞行(Y 方向等于 0)。如果球拍击中顶角,球应以 45\xc2\xb0 角向上飞行(Y 为 1,参见 ASCII 艺术),如果球拍击中底部,球应以 45\xc2\xb0 角向上飞行(Y 为 1,参见 ASCII 艺术)。 xc2\xb0 向下的角度(Y 为 -1,请参见 ASCII 艺术)。45\xc2\xb0 角度(向上或向下)是通过具有相同绝对值的速度的 X 和 Y 分量来实现的。介于两者之间的任何值都会产生介于两者之间的值。
由于这种行为与球拍的尺寸无关,因此球到球拍中心的距离除以球拍的长度(实际上我认为它应该是尺寸的一半,因为否则顶部会是.5 和底部 -.5)。
\n