private void FireShotgun() { StartCoroutine(OpenFireEffect()); for (int i = 0; i < weaponInfo.GetShotgunPalletCount(); i++) { Vector3 randomVector = Random.insideUnitCircle * weaponInfo.GetShotgunSpread(); Ray ray = new Ray(firePos.position + firePos.forward * 1f + randomVector, firePos.forward); RaycastHit hit; if (Physics.Raycast(ray, out hit, weaponInfo.range)) { AIStatus aiStatus = hit.collider.GetComponentInParent <AIStatus>(); if (aiStatus != null) { aiStatus.TookHit(hit.collider.transform, weaponInfo.damage); GameObject bloodEffect = Instantiate(aiStatus.bloodEffect, hit.point, Quaternion.identity) as GameObject; float playLength = 1f; //TODO: Change this to particle play length; Destroy(bloodEffect, playLength); } else { GameObject bulletHole = Instantiate(weaponInfo.bulletHole, hit.point + (hit.normal * 0.001f), Quaternion.FromToRotation(Vector3.forward, hit.normal)); Destroy(bulletHole, 3f); } } } DecreaseMagazine(); IncreaseRecoilRadius(); }
protected virtual void Following() { Vector3 ToEnemyDistance = (following.transform.position - transform.position); //Vector3 direction = ToEnemyDistance.normalized; if (ToEnemyDistance.sqrMagnitude >= CircleRadius) { followingCharacter = null; Status = AIStatus.BackToStart; } else { if (ToEnemyDistance.x > DistanceToAttack) { RunRight(); } else if (ToEnemyDistance.x < -DistanceToAttack) { RunLeft(); } else { Status = AIStatus.Attacking; CanAttack = true; } } }
private void ToChase() { if (MyBlockNum <= 10) { return; } var enemyList = new List <CharacterBase>(); for (int i = 0; i < stageMng.allCharacters.Count; i++) { if (stageMng.allCharacters[i] != this) { var dis = Vector3.Distance(stageMng.allCharacters[i].transform.position, transform.position); if (dis <= enemySearchDis) { enemyList.Add(stageMng.allCharacters[i]); } } } if (enemyList.Count > 0) { enemyList.Sort((a, b) => (int)Vector3.Distance(a.transform.position, transform.position) - (int)Vector3.Distance(b.transform.position, transform.position)); targetEnemy = enemyList[0]; nowRoot = GetChaseEnemy(targetEnemy); ai = AIStatus.Chase; } else { if (ai != AIStatus.Search) { ToSearch(); } } }
private void ResumePatrolling() { Debug.Log("AI lost interest - resuming patrol"); aiStatus = AIStatus.Patrol; FindClosestWaypoint(); UpdateNavMeshAgentDestinationPosition(); }
public override void Init() { base.Init(); bornPosition = transform.position; Utils.GroundTransform(transform, 999f); energyadd = (int)(Energy.CREEP_ENERGY_AV*energyCoef); aiStatus = AIStatus.disable; }
void Start() { status = GetComponent <AIStatus>(); navi = GetComponent <NavMeshAgent>(); anim = GetComponent <Animator>(); fight = GetComponent <AIFight>(); leftHandTrigger.enabled = false; rightHandTrigger.enabled = false; }
void Start() { canMove = true; _aiCurrentStatus = AIStatus.Patrol; _faceDirection = FaceDirection.Right; targetTrans = GameObject.FindGameObjectWithTag("Player").transform; enemyAnimator = GetComponent <AnimationController>(); }
// Use this for initialization void Start() { animator = GetComponent <Animator>(); aiStatus = GetComponent <AIStatus>(); audioSource = GetComponent <AudioSource>(); int random = Random.Range(0, idleClips.Length); audioSource.clip = idleClips[random]; audioSource.Play(); }
public void IsStatus(AIStatus status) { if (memory.status == status) { Task.current.Succeed(); } else { Task.current.Fail(); } }
public override void Init() { //runAwayDist = attackDist * 1.4f; base.Init(); Parameters.Parameters[ParamType.Speed] = GreatRandom.RandomizeValue(Parameters.Parameters[ParamType.Speed]); bornPosition = transform.position; Utils.GroundTransform(transform, 999f); //curWeapon.power = GreatRandom.RandomizeValue(curWeapon.power); moneyCollect = GreatRandom.RandomizeValue(moneyCollect); energyadd = GreatRandom.RandomizeValue(energyadd); aiStatus = AIStatus.disable; }
IEnumerator Sawing() { status = AIStatus.Idle; while (true) { animation.CrossFade(SawingAnimation); yield return(new WaitForSeconds(Random.Range(3f, 6f))); animation.CrossFade(TakeABreakAnimation); yield return(new WaitForSeconds(animation[TakeABreakAnimation].length + 0.1f)); } }
// Use this for initialization void Start() { audioSource = transform.GetComponent <AudioSource>(); aiStatus = GetComponent <AIStatus>(); aiController = GetComponent <AIController>(); tf = GetComponent <Transform>(); velocity = new Vector3(0, 0, 0); //startingPosition = tf.position; nav = transform.GetComponent <NavMeshAgent>(); grav = 7.84f; bounceMod = 0.35f; }
public override void CheckStatus() { float magnitude = (this.player.get_position() - this.get_transform().get_position()).get_magnitude(); if (magnitude < this.scaredDistance) { this.status = AIStatus.Scared; } else if (magnitude > this.awareDistance) { this.status = AIStatus.Idle; } }
public DoodleFlingerBot() { this.awareDistance = 15f; this.scaredDistance = 10f; this.runSpeed = 500f; this.turn = 1f; this.boing = 25f; this.gravity = 20f; this.speed = 6f; this.jumpSpeed = 8f; this.status = AIStatus.Idle; this.moveDirection = Vector3.get_zero(); }
public override void Update() { this.CheckStatus(); AIStatus aIStatus = this.status; if (aIStatus == AIStatus.Idle) { this.Idle(); } else if (aIStatus == AIStatus.Scared) { this.RunAway(); } }
protected override void VerifyActionsOnUpdate() { if (following == null || !following.activeInHierarchy || !followingCharacter.Alive) { if (Status != AIStatus.Idle) { Status = AIStatus.BackToStart; followingCharacter = null; } } else if (followingCharacter != null) { Status = AIStatus.Following; } #region STATE MACHINE if (Status == AIStatus.BackToStart) { BackToStart(); } if (Status == AIStatus.Following) { Following(); } if (Status == AIStatus.Attacking) { Attacking(); } if (Status == AIStatus.Idle) { AIIdle(); } #endregion if (IsWalking && CanJump && Time.frameCount % 5 == 0) { if (Mathf.Abs(LastX - transform.position.x) < DifferenceToJump) { Jump(); CanJump = false; StartCoroutine(WaitToJump()); } } LastX = transform.position.x; }
public async Task Run(double exp, double rmv, decimal addThr, decimal rmvThr, double brk) { Status = AIStatus.Working; ResetProperties(exp, rmv, addThr, rmvThr, brk); var it = 0; while (it++ < 300 && !_stop) { _possibleExpansions = new List <Tuple <Tuple <int, int>, decimal> >(); Tuple <int, int> choice = null; if (_rnd.NextDouble() < _expansionChance) { await NewExpansion(); choice = GetChoice(); if (choice != null) { await _controller.AddBlock(choice.Item1, choice.Item2); } } else if (_rnd.NextDouble() < _removeChance && _placedBlocks.Count > 0) { await RemoveBlock(); choice = GetChoice(true); if (choice != null) { await _controller.RemoveBlock(choice.Item1, choice.Item2); } } else { await ExpandExisting(); choice = GetChoice(); if (choice != null) { await _controller.AddBlock(choice.Item1, choice.Item2); } } await AddBlock(choice); } Status = (_stop) ? AIStatus.Stopped : AIStatus.Finished; }
IEnumerator Attack() { status = AIStatus.Attacking; while (true) { //Find the alien creature to attack Collider[] colliders = Util.GetObjectsInLayer("xenz", this.transform.position, RunToOffenseDistance); if (colliders != null && colliders.Length > 0) { currentTarget = colliders[0].transform; // currentTarget = Xenz; float distance = Util.DistanceOfCharactersXZ(this.controller, currentTarget.GetComponent <CharacterController>()); //If distance not close enough to attack if (distance > AttackRadius) { if (distance > CautiousOffenseDistance) { if (CanMove()) { Util.MoveTowards(this.transform, currentTarget.position, this.controller, true, false, this.RunToOffenseSpeed, 0); animation.CrossFade(OffenseRunAnimation); } } else { if (CanMove()) { Util.MoveTowards(this.transform, currentTarget.position, this.controller, true, false, this.WalkToOffenseSpeed, 0); animation.CrossFade(WalkAnimation); } } } //close enough, let attack begin! else { animation.Stop(NormalWalkAnimation); string _AttackAnimation = Util.RandomFromArray(this.AttackAnimations); animation.CrossFade(_AttackAnimation); float extendWaitSeconds = 0.0f; yield return(new WaitForSeconds(animation[_AttackAnimation].length + extendWaitSeconds)); //Attack over, now let the guy slip away for a short time yield return(StartCoroutine(SlipAway())); } } yield return(null); } }
void CheckStatus() { float dist = (player.position - transform.position).magnitude; if (dist < scaredDistance) { status = AIStatus.scared; } else if (dist >= scaredDistance && dist <= awareDistance) { status = AIStatus.aware; } else if (dist > awareDistance) { status = AIStatus.idle; } }
protected virtual void BackToStart() { float ToStartDistance = DistanceToStartPoint.x; if (ToStartDistance > 1f) { RunRight(); } else if (ToStartDistance < -1f) { RunLeft(); } else { Status = AIStatus.Idle; OnAIIdle(); } }
private void FixedUpdate() { // Raycast for walls checkWalls = Physics2D.Raycast(transform.position, transform.right, 1f, patrolLayer); // Raycast for Player hit2D = Physics2D.Raycast(attackTrans.position, transform.right, targetDetectionRange, playerLayer); if (hit2D == true) { _aiCurrentStatus = AIStatus.Attack; } else { //Debug.DrawRay(attackTrans.position, transform.right * rayDistance, Color.blue); _aiCurrentStatus = AIStatus.Patrol; Debug.DrawRay(transform.position, transform.right * targetDetectionRange, Color.blue); } }
protected override void VerifyActionsOnFixedUpdate() { base.VerifyActionsOnFixedUpdate(); if (Time.frameCount % DetectionFrameRate != 0) { return; } RaycastHit2D hit = Physics2D.CircleCast(transform.position + new Vector3(CircleOffsetX, CircleOffsetY), CircleRadius, Vector2.right, 0f); if (hit) { CharacterStrategy externalCharacter = hit.collider.gameObject.GetComponent <CharacterStrategy>(); if (externalCharacter != null && externalCharacter.Ally != character.Ally) { followingCharacter = externalCharacter; Status = AIStatus.Following; } } }
private void Awake() { stat_p = GetComponent <AIStatus>(); anim_p = GetComponent <Animator>(); target[0] = gameObject.transform.Find("Monster"); target[1] = gameObject.transform.Find("BossMonster"); AIState[] stateValues = (AIState[])System.Enum.GetValues(typeof(AIState)); foreach (AIState s in stateValues) { System.Type FSMType = System.Type.GetType("AI" + s.ToString()); AIFSMState state = (AIFSMState)GetComponent(FSMType); if (null == state) { state = (AIFSMState)gameObject.AddComponent(FSMType); } state_d.Add(s, state); state.enabled = false; } }
void SetAnimStatus(AIStatus status) { switch (status) { case AIStatus.Calm: agent.speed = memory.walk_speed; memory.anim.SetBool("suspicious", false); memory.anim.SetBool("hostile", false); break; case AIStatus.Suspicious: agent.speed = memory.walk_speed; memory.anim.SetBool("suspicious", true); memory.anim.SetBool("hostile", false); break; case AIStatus.Hostile: agent.speed = memory.run_speed; memory.anim.SetBool("suspicious", true); memory.anim.SetBool("hostile", true); break; } }
public void AttackVigilanceCancel() { if (ai == AIStatus.Avoidance) { if (targetAttackObj.Master.IsDeath == false) { var dis = Vector3.Distance(transform.position, targetAttackObj.Master.transform.position); if (dis <= enemySearchDis * 2) { targetEnemy = targetAttackObj.Master; ai = AIStatus.Chase; } else { ToSearch(); } } else { ToSearch(); } targetAttackObj = null; } }
// Use this for initialization void Start() { viewAngleRad = viewAngleDeg*Mathf.Deg2Rad; controller = transform.GetComponent<CharacterController>(); Random.seed = 1; //Default behaviour currBehaviour = new BWander(this); //Default destination destination = transform.position; wanderTarget = new Vector3(0,0,0); velocity = new Vector3(0,0,0); //Set up obstacle avoidance feelers feelers = new Vector3[5]; feelers[0] = new Vector3(0,0,1); feelers[1] = Quaternion.Euler(0,22,0)* feelers[0]; feelers[2] = Quaternion.Euler(0,-22,0)* feelers[0]; feelers[3] = Quaternion.Euler(0,45,0)* feelers[0]; feelers[4] = Quaternion.Euler(0,-45,0)* feelers[0]; //Initialize enemies GameObject tmp = GameObject.Find("EnemySpartan"); if (tmp != null){ spartan=tmp.transform; enemyStatus = spartan.GetComponent(typeof(AIStatus)) as AIStatus; } tmp = GameObject.Find("TheDragon"); if (tmp != null){ dragon=tmp.transform; } //Turn off the gun for now gun.gameObject.SetActiveRecursively(false); }
void Start() { pathCtrl = transform.parent.GetComponentInChildren <PatrolPathController>(); AI_status = pathCtrl ? AIStatus.PATROL : AIStatus.WAIT; }
private void ToSearch() { ai = AIStatus.Search; nowRoot = GetSearchRoot(); }
private void ToRun() { ai = AIStatus.Run; nowRoot = GetRunRoot(); }
private void EndAttack() { // Debug.Log("Run AWAY " + Parameters.AttackType); aiStatus = AIStatus.returnHome; if (Action != null) { Action.End(); } Action = new MoveAction(this, bornPosition, EndWalk); }
private void StartWalk() { aiStatus = AIStatus.walk; int coef = 60; if (Action is MoveAction) { coef = 30; } var shalgo = UnityEngine.Random.Range(0, 100) < coef; if (shalgo) { var randPos = new Vector3(bornPosition.x + UnityEngine.Random.Range(-isHomeDist, isHomeDist), bornPosition.y, bornPosition.z + UnityEngine.Random.Range(-isHomeDist, isHomeDist)); Action = new MoveAction(this, randPos, StartWalk); } else { Action = new StayAction(this, StartWalk); } }
public void DoAction(float delta, World world) { if (!alive) { return; } // Checks here if (world.map[position.x][position.y].water) { status = AIStatus.Move; } List <Creation> creations = searchCreations(world); if (creations.Count > 0) { status = AIStatus.Follow; } if (world.map[position.x][position.y].grass && !world.map[position.x][position.y].city && politStatus == PoliticalStatus.Builder) { status = AIStatus.Build; } else { if (creations.Count > 0 && politStatus == PoliticalStatus.Soldier) { status = AIStatus.Follow; } else { status = AIStatus.Move; } } if (full <= 20 && world.map[position.x][position.y].city) { status = AIStatus.Eat; } if (full <= 0) { alive = false; return; } Random random = new Random(); int followIndex = random.Next(creations.Count); //Actions here switch (status) { case AIStatus.Move: if (politStatus == PoliticalStatus.Civilian) { Vector2 cityPos = SearchCity(world); Vector2 directionCity; if (cityPos != null) { directionCity = GetDirection(cityPos); } else { directionCity = new Vector2(random.Next(-1, 2), random.Next(-1, 2)); } position.x += directionCity.x; position.y += directionCity.y; } else if (politStatus == PoliticalStatus.Builder) { position.x += random.Next(-1, 2); position.y += random.Next(-1, 2); } full -= 0.05f; break; case AIStatus.Follow: Vector2 direction = GetDirection(creations[followIndex].position); position.x += direction.x; position.y += direction.y; break; case AIStatus.Build: progress += speedBuid * delta; if (progress >= 100) { if (world.map[position.x][position.y].road) { world.map[position.x][position.y].city = true; } else { world.map[position.x][position.y].road = true; } progress = 0; world.IncreaseTens(0.01f); } break; case AIStatus.Eat: if (world.map[position.x][position.y].city) { full += eatSpeed * delta; } break; } }
public void DoAction(float delta, World world) { if (!alive) { return; } CheckDetector(world); // Checks here if (world.map[position.x][position.y].status == PixelStatus.Water) { status = AIStatus.Move; } //List<Creation> creations = searchCreations(world); /*if (detectorLocation.creations.Count > 0) { * status = AIStatus.Follow; * }*/ if (politStatus == PoliticalStatus.Civilian && detectorLocation.creations.Count > 1) { status = AIStatus.Reproduce; } if ((world.map[position.x][position.y].status == PixelStatus.Grass || world.map[position.x][position.y].status == PixelStatus.Road) && politStatus == PoliticalStatus.Builder) { status = AIStatus.Build; } else if (politStatus == PoliticalStatus.Civilian && world.map[position.x][position.y].status != PixelStatus.City) { status = AIStatus.Move; } else if (politStatus == PoliticalStatus.Builder || politStatus == PoliticalStatus.Soldier) { status = AIStatus.Move; } if (full <= 35 && world.map[position.x][position.y].status == PixelStatus.City) { status = AIStatus.Eat; } if (full <= 0) { if (politStatus == PoliticalStatus.Soldier) { Console.WriteLine("Soldier dies from starvation"); } alive = false; return; } Random random = new Random(); //int followIndex = random.Next(creations.Count); //Actions here switch (status) { case AIStatus.Move: if (politStatus == PoliticalStatus.Civilian) { MoveToCity(world); } else if (politStatus == PoliticalStatus.Builder) { /*if (destination == null) { * destination = SearchGrass(world); * } * if (destination != null) { * moveOnProgress(GetDirection(destination)); * }*/ //else { moveOnProgress(new Vector2(random.Next(-1, 2), random.Next(-1, 2))); //} } else if (politStatus == PoliticalStatus.Soldier) { full = 100; if (destination == null) { destination = SearchEnemy(world); } if (destination != null) { moveOnProgress(GetDirection(destination)); } foreach (Creation creation in detectorLocation.creations) { if (creation.country != country && creation.country != null && country != null) { if (!country.warLevel.TryGetValue(creation.country, out byte level)) { continue; } progress += (float)random.NextDouble() * mind; if (progress > 100) { creation.alive = false; progress = 0; world.IncreaseTens(0.001f, country); if (!country.warLevel.ContainsKey(creation.country)) { country.warLevel.Add(creation.country, 1); } else { if (country.warLevel[creation.country] < byte.MaxValue) { country.warLevel[creation.country]++; } } } } } } break; /*case AIStatus.Follow: * Vector2 direction = GetDirection(creations[followIndex].position); * position.x += direction.x; * position.y += direction.y; * break;*/ case AIStatus.Build: progress += speedBuid * delta; if (progress >= 100) { if (world.map[position.x][position.y].status == PixelStatus.Road) { world.map[position.x][position.y].status = PixelStatus.City; } else { world.map[position.x][position.y].status = PixelStatus.Road; } progress = 0; world.IncreaseTens(0.01f, country); } break; case AIStatus.Eat: if (world.map[position.x][position.y].status == PixelStatus.City) { full += eatSpeed * delta; } else { MoveToCity(world); } break; case AIStatus.Reproduce: progress += 1.0f * delta; if (progress >= 100 && detectorLocation != null) { Creation child = new Creation(position, mind + 2); child.country = country; detectorLocation.creations.Add(child); Console.WriteLine("Detector population is now {0}", detectorLocation.creations.Count); progress -= 100; world.IncreaseTens(0.005f, country); } break; } }
IEnumerator Attack() { status = AIStatus.Attacking; while (true) { //Find the alien creature to attack Collider[] colliders = Util.GetObjectsInLayer("xenz", this.transform.position, RunToOffenseDistance); if (colliders != null && colliders.Length > 0) { currentTarget = colliders[0].transform; // currentTarget = Xenz; float distance = Util.DistanceOfCharactersXZ(this.controller, currentTarget.GetComponent<CharacterController>()); //If distance not close enough to attack if (distance > AttackRadius) { if (distance > CautiousOffenseDistance) { if (CanMove()) { Util.MoveTowards(this.transform, currentTarget.position, this.controller, true, false, this.RunToOffenseSpeed, 0); animation.CrossFade(OffenseRunAnimation); } } else { if (CanMove()) { Util.MoveTowards(this.transform, currentTarget.position, this.controller, true, false, this.WalkToOffenseSpeed, 0); animation.CrossFade(WalkAnimation); } } } //close enough, let attack begin! else { animation.Stop(NormalWalkAnimation); string _AttackAnimation = Util.RandomFromArray(this.AttackAnimations); animation.CrossFade(_AttackAnimation); float extendWaitSeconds = 0.0f; yield return new WaitForSeconds(animation[_AttackAnimation].length + extendWaitSeconds); //Attack over, now let the guy slip away for a short time yield return StartCoroutine(SlipAway()); } } yield return null; } }
IEnumerator Sawing() { status = AIStatus.Idle; while (true) { animation.CrossFade(SawingAnimation); yield return new WaitForSeconds(Random.Range(3f, 6f)); animation.CrossFade(TakeABreakAnimation); yield return new WaitForSeconds(animation[TakeABreakAnimation].length + 0.1f); } }
// Checks to see if the enemy referenced by the variable "target" can be seen. Don't forget to set the target before calling this - // or modify the function and pass the target to it public bool EnemySeen() { if (spartan == null) { return false; } Vector3 toPlayer = spartan.position - transform.position; float dist = toPlayer.magnitude; toPlayer.y = 0; toPlayer = Vector3.Normalize(toPlayer); //Forward in world space Vector3 forward = transform.TransformDirection(new Vector3(0, 0, 1)); forward.y = 0; forward = Vector3.Normalize(forward); if (enemyStatus == null) { enemyStatus = spartan.GetComponent(typeof(AIStatus)) as AIStatus; } //print(enemyStatus.isAlive()); //print(Vector3.Dot(toPlayer, forward)); //print(Mathf.Cos(viewAngleRad)); //If the player is no more than 60 degrees away from forward - i.e. if player within 120 degree view if (spartan.gameObject.active == true && enemyStatus.isAlive() && (Vector3.Dot(toPlayer, forward) >= Mathf.Cos(viewAngleRad))) { return true; } else return false; }
private void ReturnHome() { aiStatus = AIStatus.returnHome; if (Action != null) { Action.End(); } Action = new MoveAction(this, bornPosition, EndComeHome); }
public void NewStatus(AIStatus s) { this.status = s; }
public void Disable() { isDisabled = true; if (Action != null) { Action.Stop(); Control.Stop(false); Action = null; //Maybe here set animation } aiStatus = AIStatus.disable; }
private void StartAttack() { aiStatus = AIStatus.attack; switch (Parameters.AttackType) { case AttackType.hitAndRun: Action = new AttackHitAndRun(this, MainController.Instance.level.MainHero, StartAttack); break; case AttackType.distanceFight: Action = new AttackDistance(this, MainController.Instance.level.MainHero, StartAttack); break; case AttackType.closeCombat: Action = new AttackCloseCombat(this, MainController.Instance.level.MainHero, StartAttack); break; } }
protected virtual void StartAttack(bool byHit) { var heroTarget = MainController.Instance.level.MainHero; if (heroTarget == null) { return; } aiStatus = AIStatus.attack; switch (Parameters.AttackType) { case AttackType.distanceFight: Action = new AttackDistance(this, heroTarget, EndAttack, byHit); break; case AttackType.closeCombat: Action = new AttackCloseCombat(this, heroTarget, EndAttack, byHit); break; } }
private void StartFollowing() { Debug.Log("Spotted player while patrolling! Commence follow!"); aiStatus = AIStatus.Follow; }