//切换状态后的行动 public override void Act(GameObject player, GameObject target, GameObject npc, Animator anime) { anime.SetBool("attack", false); Vector2 rdDirection = new Vector2(Random.value, Random.value).normalized; while (_time > 2f) { if (_path != null) { _path.RemoveAt(0); } _path = NavMesh2D.GetSmoothedPath(npc.transform.position, target.transform.position); _time = 0f; } if (_path != null && _path.Count != 0) { npc.transform.position = Vector2.MoveTowards(npc.transform.position, _path[0], 1.5f * Time.deltaTime); //npc.GetComponent<Rigidbody2D>().velocity = new Vector2(npc.transform.position.x - _path[0].x, npc.transform.position.y - _path[0].y).normalized; if (Vector2.Distance(npc.transform.position, _path[0]) < 0.8f) { _path.RemoveAt(0); } } _time += Time.fixedDeltaTime; }
public bool SetTarget(Vector3 t) { if (enabled && NavMesh2D.GetNavMeshObject()) { pathingTarget = NearestPoint(t, NavMesh2D.GetNavMeshObject().NavMesh); path = NavMesh2D.GetSmoothedPath(transform.position, pathingTarget); if (path.Count <= 0) { Vector3?cn = ClosestNode(t); if (cn == null) { return(false); } pathingTarget = (Vector3)cn; path = NavMesh2D.GetSmoothedPath(transform.position, pathingTarget); } Vector2 vk = Vector2.zero; for (int i = 0; i < path.Count; i++) { vk = path[i]; } if (path != null && path.Count > 0) { velocity = lastVelBeforeZero; } } return(true); }
// // void OnCollisionStay2D(Collision2D coll) // { // if(coll.gameObject.tag == Tags.enemy ) // { // timer = 0f; // timer += Time.deltaTime; // if(timer > 8f ) // this.targeting = true; // // print("collision staying" + timer); // } // } // Update is called once per frame void FixedUpdate() { if (tankHero == null) { return; } if (Time.deltaTime == 0f) { return; } var targetPoint = this.tankHero.transform.position; targetPoint.z = this.transform.position.z; if (this.targeting) { var direction = (targetPoint - this.transform.position).normalized; base.movingTarget = targetPoint + direction * 0.25f; if (this.targetFlag != null) { if (this.lastTargetFlag != null) { Destroy(this.lastTargetFlag.gameObject); } lastTargetFlag = Instantiate(targetFlag, base.movingTarget, this.transform.rotation) as Transform; path = NavMesh2D.GetSmoothedPath(this.transform.position, lastTargetFlag.position); } this.targeting = false; } if (path != null && path.Count != 0) { this.transform.position = Vector2.MoveTowards(this.transform.position, path [0], 2f * Time.deltaTime); if (Vector2.Distance(transform.position, path [0]) < 0.01f) { path.RemoveAt(0); } } base.fireTarget = targetPoint; base.baseDirection = (base.movingTarget - this.transform.position).normalized; // this.transform.position += base.baseDirection * base.speed * Time.deltaTime; if ((this.transform.position - base.movingTarget).magnitude < 2f) { this.targeting = true; } //Debug.Log (string.Format ("{0} -> {1} | {2}", this.transform.position, this.targetPosition, this.targeting)); }
void FindTarget() { if (pathingTarget == null) { pathingTarget = BattleManager.Instance.GetPathingTarget(unit.m_EnemyCamp, unit.m_UnitInfo); } if (pathingTarget != null && m_bFindPath) { if (m_bFindPath) { StartCoroutine(ComponentTools.SetWaitTime(0.3f, () => { m_bFindPath = true; })); } path = NavMesh2D.GetSmoothedPath(transform.position, pathingTarget.transform.position); m_bFindPath = false; //if (pathingTarget.transform.position != lastTargetPos) //{ // path = NavMesh2D.GetSmoothedPath(transform.position, pathingTarget.transform.position); // lastTargetPos = pathingTarget.transform.position; //} } if (path != null && path.Count != 0) { transform.position = Vector2.MoveTowards(transform.position, path[0], unit.m_UnitInfo.speed * Time.deltaTime); float fDis = Vector2.Distance(transform.position, path[0]); if (fDis < 0.01f) { path.RemoveAt(0); } //if (path.Count == 1) //{ // if (Mathf.Abs(transform.position.x - path[0].x) < 0.1f) // { // path.RemoveAt(0); // } //} //else //{ // float fDis = Vector2.Distance(transform.position, path[0]); // if (fDis < 0.1f) // { // path.RemoveAt(0); // } //} } }
// LateUpdate is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.E)) { path = NavMesh2D.GetSmoothedPath(transform.position, pathingTarget.position); } if (path != null && path.Count != 0) { transform.position = Vector2.MoveTowards(transform.position, path[0], 5 * Time.deltaTime); if (Vector2.Distance(transform.position, path[0]) < 0.01f) { path.RemoveAt(0); } } }
IEnumerator createPath() { Vector2 target; while (true) { if (canSeePlayer) { target = player.position; currentState = State.chase; } else { target = waypoints[nextWaypoint].position; currentState = State.goBack; } path = NavMesh2D.GetSmoothedPath(transform.position, target); yield return(new WaitForSeconds(0.1f)); } }
void FixedUpdate() { if (hacked) { Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 10); foreach (Collider2D hit in colliders) { //Debug.Log("hits feitos"); if (hit.gameObject.tag == "enemy") { timer += Time.deltaTime; if (timer > waitingTime) { GameObject dynamicParent = GameObject.Find("DynamicObjects"); Transform newObject = Instantiate(bulletHacked, new Vector3(rb.position.x, rb.position.y, 0), Quaternion.identity) as Transform; newObject.parent = dynamicParent.transform; Debug.Log("success em principio"); timer = 0; } } } } else { if (health <= 0) { Instantiate(ammo, new Vector3(rb.position.x, rb.position.y, 0), Quaternion.identity); Destroy(gameObject); } if (calm) { if (patrolDest.Equals(Vector3.zero)) { GameObject patrolPoint = patrolPoints[Random.Range(0, patrolPoints.Length)]; patrolDest = patrolPoint.transform.position; path = NavMesh2D.GetSmoothedPath(transform.position, patrolDest); Debug.Log("RESET"); } else { float playerDist = Vector2.Distance(transform.position, patrolDest); if (playerDist < 0.5f) { patrolDest = Vector3.zero; } if (path != null && path.Count != 0) { moveToPosition(new Vector3(path[0].x, path[0].y, 0)); //Debug.Log(path[0].x); //Debug.Log(path[0].y); //Debug.Log(Vector2.Distance(transform.position, path[0])); if (Vector2.Distance(transform.position, path[0]) < 0.1f) { path.RemoveAt(0); Debug.Log("REMOVED"); } } } } else { float playerDist = Vector2.Distance(transform.position, player.transform.position); if (playerDist > 5) { path = NavMesh2D.GetSmoothedPath(transform.position, player.transform.position); if (path != null && path.Count != 0) { if (currentPoint == 0) { moveToPosition(new Vector3(path[1].x, path[1].y, 0)); } if (Vector2.Distance(transform.position, path[currentPoint + 1]) < 0.01f) { currentPoint++; moveToPosition(new Vector3(path[currentPoint].x, path[currentPoint].y, 0)); } } } else { rb.velocity = Vector2.zero; rb.angularVelocity = 0; Vector3 playerDir = player.transform.position - transform.position; transform.rotation = Quaternion.LookRotation(Vector3.forward, playerDir); } } bool tr = canSeePlayer(); if (tr) { calm = false; timer += Time.deltaTime; Debug.Log(timer); if (timer > waitingTime) { GameObject dynamicParent = GameObject.Find("DynamicObjects"); Transform newObject = Instantiate(bullet, new Vector3(rb.position.x, rb.position.y, 0), Quaternion.identity) as Transform; newObject.parent = dynamicParent.transform; Debug.Log("SHOOT"); timer = 0; } } } }