我正在学习教程,我理解了其中的大部分内容。我想问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
// …Run Code Online (Sandbox Code Playgroud) 我收到上述错误,我尝试使用debug.log打印错误的位置.我正在创造一种坦克obj.哪个实例化了obj.实例化对象是攻击者.
在更新中,我使用foreach循环遍历所有实例化对象.如果找到并且If对象在射程范围内.
void Update () {
if (attacker!= null )
{
//Debug.Log("inside att");
attacker = FindObjectsOfType<Attacker>();
}
// fire only if attacker is in range
if (IfInRange() && attacker!= null && running)
{
faceTowardTarget();
Fire();
}
}
bool IfInRange()
{// run through all the instantiated attacker
foreach (Attacker currentAttacker in attacker)
Run Code Online (Sandbox Code Playgroud)
这工作正常,但有些时候会给出上面的结果.在控制台的最后,循环继续进行,currentAttacker最后为空.我试着在控制台中打印它.但它不会进入其他if语句
{ //if attacker is in range
if (attacker != null )
{
Debug.Log(currentAttacker.GetComponent<Health>().isAlive);
if (Vector2.Distance(transform.position, currentAttacker.transform.position) < minDistance)
{
attackerPos = currentAttacker.transform;
return true;
}
}
if (currentAttacker …Run Code Online (Sandbox Code Playgroud)