public Vector3 getSteering(ICollection <MovementAIRigidbody> targets) { Vector3 centerOfMass = Vector3.zero; int count = 0; /* Sums up everyone's position who is close enough and in front of the character */ foreach (MovementAIRigidbody r in targets) { if (steeringBasics.isFacing(r.position, facingCosineVal)) { centerOfMass += r.position; count++; } } if (count == 0) { return(Vector3.zero); } else { centerOfMass = centerOfMass / count; return(steeringBasics.arrive(centerOfMass)); } }
public Vector3 GetSteering(ICollection <Rigidbody> targets) { Vector3 centerOfMass = Vector3.zero; int count = 0; /* 得到我前方视野内所有角色的 中心 */ foreach (Rigidbody r in targets) { // 在视野内 (视野是 无限扇形) if (steeringBasics.isFacing(r.position, facingCosineVal)) { centerOfMass += r.position; count++; } } if (count == 0) // 我前面没有人。 漫游好了 { return(Vector3.zero); } else { // 目标目标位置 centerOfMass = centerOfMass / count; return(steeringBasics.Arrive(centerOfMass)); } }
public Vector3 GetSteering(ICollection <Rigidbody> targets) { Vector3 accel = Vector3.zero; int count = 0; // 得到我视野内 所有人的 加速度平均值 foreach (Rigidbody r in targets) { if (steeringBasics.isFacing(r.position, facingCosineVal)) { /* 计算我们想要匹配这个目标的加速度 */ Vector3 a = r.velocity - rb.velocity; /* * Rather than accelerate the character to the correct speed in 1 second, * accelerate so we reach the desired speed in timeToTarget seconds * (if we were to actually accelerate for the full timeToTarget seconds). */ // 而不是在1秒内加速字符的正确速度,这样我们就能在目标秒内达到所期望的速度(如果我们要在目标秒内加速的话)。 a = a / timeToTarget; accel += a; count++; } } if (count > 0) { accel = accel / count; /* 不要超值 */ if (accel.magnitude > maxAcceleration) { accel = accel.normalized * maxAcceleration; } } return(accel); }
public Vector2 getSteering(ICollection <Rigidbody2D> targets) { Vector2 accel = Vector2.zero; int count = 0; foreach (Rigidbody2D r in targets) { if (steeringBasics.isFacing(r.position, facingCosineVal)) { /* Calculate the acceleration we want to match this target */ Vector2 a = r.velocity - rb.velocity; /* * Rather than accelerate the character to the correct speed in 1 second, * accelerate so we reach the desired speed in timeToTarget seconds * (if we were to actually accelerate for the full timeToTarget seconds). */ a = a / timeToTarget; accel += a; count++; } } if (count > 0) { accel = accel / count; /* Make sure we are accelerating at max acceleration */ if (accel.magnitude > maxAcceleration) { accel = accel.normalized * maxAcceleration; } } return(accel); }