protected override int onExecute(TBTWorkingData wData) { AIEntityWorkingData thisData = wData.As <AIEntityWorkingData>(); Vector3 targetPos = TMathUtils.Vector3ZeroY(thisData.entity.GetBBValue <Vector3>(AIEntity.BBKEY_NEXTMOVINGPOSITION, Vector3.zero)); Vector3 currentPos = TMathUtils.Vector3ZeroY(thisData.entityTF.position); if (TMathUtils.IsZero((targetPos - currentPos).sqrMagnitude)) { return(TBTRunningStatus.FINISHED); } else { Vector3 toTarget = TMathUtils.GetDirection2D(targetPos, currentPos); Vector3 curFacing = thisData.entityTF.forward; float dotV = Vector3.Dot(toTarget, curFacing); float deltaAngle = Mathf.Acos(Mathf.Clamp(dotV, -1f, 1f)); if (deltaAngle < 0.1f) { thisData.entityTF.forward = toTarget; return(TBTRunningStatus.FINISHED); } else { Vector3 crossV = Vector3.Cross(curFacing, toTarget); float angleToTurn = Mathf.Min(3f * thisData.deltaTime, deltaAngle); if (crossV.y < 0) { angleToTurn = -angleToTurn; } thisData.entityTF.Rotate(Vector3.up, angleToTurn * Mathf.Rad2Deg, Space.World); } } return(TBTRunningStatus.EXECUTING); }
protected override int onExecute(TBTWorkingData wData) { AIEntityWorkingData thisData = wData.As <AIEntityWorkingData>(); Vector3 targetPos = TMathUtils.Vector3ZeroY(thisData.entity.GetBBValue <Vector3>(AIEntity.BBKEY_NEXTMOVINGPOSITION, Vector3.zero)); Vector3 currentPos = TMathUtils.Vector3ZeroY(thisData.entityTF.position); float distToTarget = TMathUtils.GetDistance2D(targetPos, currentPos); if (distToTarget < 1f) { thisData.entityTF.position = targetPos; return(TBTRunningStatus.FINISHED); } else { int ret = TBTRunningStatus.EXECUTING; Vector3 toTarget = TMathUtils.GetDirection2D(targetPos, currentPos); float movingStep = 0.5f * thisData.deltaTime; if (movingStep > distToTarget) { movingStep = distToTarget; ret = TBTRunningStatus.FINISHED; } thisData.entityTF.position = thisData.entityTF.position + toTarget * movingStep; return(ret); } }
public override bool IsTrue(TBTWorkingData wData) { AIEntityWorkingData thisData = wData.As <AIEntityWorkingData>(); Vector3 targetPos = TMathUtils.Vector3ZeroY(thisData.entity.GetBBValue <Vector3>(AIEntity.BBKEY_NEXTMOVINGPOSITION, Vector3.zero)); Vector3 currentPos = TMathUtils.Vector3ZeroY(thisData.entityTF.position); return(TMathUtils.GetDistance2D(targetPos, currentPos) < 1f); }