// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { if (UtilsScripts.CheckPointInGameObject(gameObject, Input.mousePosition)) { m_isStart = true; Camera.main.GetComponent <TrackGameObjScript>().setTrackEnable(false); m_beforePoint = Input.mousePosition; } } else if (Input.GetMouseButton(0)) { if (m_isStart) { Vector3 nowPoint = Input.mousePosition; Vector3 moveVec3 = nowPoint - m_beforePoint; Camera.main.transform.position += (new Vector3(moveVec3.x, 0, moveVec3.y)) / 50; m_beforePoint = nowPoint; } } else if (Input.GetMouseButtonUp(0)) { if (m_isStart) { m_isStart = false; Camera.main.GetComponent <TrackGameObjScript>().setTrackEnable(true); } } }
public float getRockerAngle() { if (UtilsScripts.TwoPointDistance2D(new Vector2(0, 0), m_ball.transform.localPosition) <= m_triggerMoveLength) { return(0); } return(UtilsScripts.TwoPointAngle(new Vector2(0, 0), m_ball.transform.localPosition)); }
void attackEnemy(float attackRange, float hurt) { // 在攻击范围内寻找攻击的对象 { GameObject attackTarget = UtilsScripts.minDistance(gameObject, m_gameScript.getAllEnemyList()); if (attackTarget != null) { if (UtilsScripts.TwoPointDistance3D(gameObject.transform.position, attackTarget.transform.position) <= m_attackRange) { // 让英雄面向敌人 { float angle = UtilsScripts.TwoPointAngle(new Vector2(gameObject.transform.position.x, gameObject.transform.position.z), new Vector2(attackTarget.transform.position.x, attackTarget.transform.position.z)); gameObject.transform.localRotation = Quaternion.Euler(0, angle, 0); } if (attackTarget.tag.CompareTo("Tower") == 0) { if (attackTarget.GetComponent <TowerScript>().hurtAndIsDie(hurt)) { m_gameScript.getEnemyTowerList().Remove(attackTarget); ToastScript.createToast("摧毁地方防御塔"); } } else if (attackTarget.tag.CompareTo("Soldier") == 0) { //// 添加攻击特效 //{ // GameObject prefabs = Resources.Load("Prefabs/shouji") as GameObject; // GameObject obj = MonoBehaviour.Instantiate(prefabs); // obj.transform.position = attackTarget.transform.position + new Vector3(0, 0.2f, 0); //} if (attackTarget.GetComponent <SoldierScript>().hurtAndIsDie(hurt)) { m_gameScript.getEnemySoldierList().Remove(attackTarget); } } } } } }
// Update is called once per frame void Update() { // 更新血条位置 m_bloodProgressBar.transform.position = Camera.main.WorldToScreenPoint(gameObject.transform.position) + new Vector3(0, 150, 0); // 在攻击范围内寻找攻击的对象 { // 优先攻击小兵 { List <GameObject> enemyList; if (!m_isMyTower) { enemyList = m_gameScript.getMySoldierList(); } else { enemyList = m_gameScript.getEnemySoldierList(); } GameObject attackTarget = UtilsScripts.minDistance(gameObject, enemyList); if (attackTarget != null) { if (UtilsScripts.TwoPointDistance3D(gameObject.transform.position, attackTarget.transform.position) <= m_attackRange) { if (m_canAttack) { // 发射子弹 { GameObject prefabs = Resources.Load("Prefabs/TowerBullet") as GameObject; GameObject obj = MonoBehaviour.Instantiate(prefabs); obj.transform.position = gameObject.transform.position + new Vector3(0, 1, 0); obj.GetComponent <TowerBulletScript>().initData(attackTarget, m_isMyTower); } Invoke("attackCDEnd", m_attackCD); m_canAttack = false; } return; } } } // 其次攻击英雄 { List <GameObject> enemyList; if (!m_isMyTower) { enemyList = m_gameScript.getMyHeroList(); } else { enemyList = m_gameScript.getEnemyHeroList(); } GameObject attackTarget = UtilsScripts.minDistance(gameObject, enemyList); if (attackTarget != null) { if (UtilsScripts.TwoPointDistance3D(gameObject.transform.position, attackTarget.transform.position) <= m_attackRange) { if (m_canAttack) { // 发射子弹 { GameObject prefabs = Resources.Load("Prefabs/TowerBullet") as GameObject; GameObject obj = MonoBehaviour.Instantiate(prefabs); obj.transform.position = gameObject.transform.position + new Vector3(0, 1, 0); obj.GetComponent <TowerBulletScript>().initData(attackTarget, m_isMyTower); } Invoke("attackCDEnd", m_attackCD); m_canAttack = false; } return; } } } } }
// Update is called once per frame void Update() { if (!m_character.isGrounded) { m_character.Move(Vector3.down * 10.0f * Time.deltaTime); } // 更新血条位置 m_bloodProgressBar.transform.position = Camera.main.WorldToScreenPoint(gameObject.transform.position) + new Vector3(0, 90, 0); { if (m_animator.GetCurrentAnimatorStateInfo(0).IsName("attack")) { m_animator.speed = 1.5f; } else { m_animator.speed = 1.0f; } } bool canMove = true; List <GameObject> enemyList; if (m_isEnemy) { enemyList = m_gameScript.getAllMyList(); } else { enemyList = m_gameScript.getAllEnemyList(); } if (enemyList.Count == 0) { if (m_isEnemy) { gameObject.transform.localRotation = Quaternion.Euler(0, -90, 0); } else { gameObject.transform.localRotation = Quaternion.Euler(0, 90, 0); } m_character.Move(gameObject.transform.forward * (Time.deltaTime * m_speed / (1.0f / 60.0f))); return; } // 在攻击范围内寻找攻击的对象 { GameObject attackTarget = UtilsScripts.minDistance(gameObject, enemyList); if (attackTarget != null) { if (UtilsScripts.TwoPointDistance3D(gameObject.transform.position, attackTarget.transform.position) <= m_attackRange) { canMove = false; if (m_canAttack) { m_animator.SetTrigger("attack"); // 让英雄面向敌人 { float angle = UtilsScripts.TwoPointAngle(new Vector2(gameObject.transform.position.x, gameObject.transform.position.z), new Vector2(attackTarget.transform.position.x, attackTarget.transform.position.z)); gameObject.transform.localRotation = Quaternion.Euler(0, angle, 0); } if (attackTarget.tag.CompareTo("Tower") == 0) { if (attackTarget.GetComponent <TowerScript>().hurtAndIsDie(m_attack)) { if (m_isEnemy) { m_gameScript.getMyTowerList().Remove(attackTarget); ToastScript.createToast("我方防御塔被摧毁"); } else { m_gameScript.getEnemyTowerList().Remove(attackTarget); ToastScript.createToast("摧毁地方防御塔"); } } } else if (attackTarget.tag.CompareTo("Soldier") == 0) { if (attackTarget.GetComponent <SoldierScript>().hurtAndIsDie(m_attack)) { if (m_isEnemy) { m_gameScript.getMySoldierList().Remove(attackTarget); } else { m_gameScript.getEnemySoldierList().Remove(attackTarget); } } } else if (attackTarget.tag.CompareTo("Hero") == 0) { if (attackTarget.GetComponent <Hero01Script>().hurtAndIsDie(m_attack)) { m_gameScript.getMyHeroList().Remove(attackTarget); ToastScript.createToast("您已阵亡"); } } m_canAttack = false; } } // 如果有敌方小兵距离自己3倍攻击范围之内的,则主动向这个小兵方向移动 else if (UtilsScripts.TwoPointDistance3D(gameObject.transform.position, attackTarget.transform.position) <= (m_attackRange * 3)) { // 让小兵面向敌人移动 { float angle = UtilsScripts.TwoPointAngle(new Vector2(gameObject.transform.position.x, gameObject.transform.position.z), new Vector2(attackTarget.transform.position.x, attackTarget.transform.position.z)); gameObject.transform.localRotation = Quaternion.Euler(0, angle, 0); } } else { if (m_isEnemy) { gameObject.transform.localRotation = Quaternion.Euler(0, -110, 0); } else { gameObject.transform.localRotation = Quaternion.Euler(0, 70, 0); } } } } if (canMove) { m_character.Move(gameObject.transform.forward * (Time.deltaTime * m_speed / (1.0f / 60.0f))); } }
// Update is called once per frame void Update() { if (m_useMouse) { Vector2 touchPos = Input.mousePosition; if (Input.GetMouseButtonDown(0)) { if (UtilsScripts.TwoPointDistance2D(touchPos, m_bg.transform.position) <= m_bgRadius) { m_isStartMove = true; m_ball.transform.position = touchPos; } else if (UtilsScripts.TwoPointDistance2D(touchPos, new Vector2(0, 0)) <= 500) { gameObject.transform.position = touchPos; m_isStartMove = true; m_ball.transform.position = touchPos; } } else if (Input.GetMouseButtonUp(0)) { if (m_isStartMove) { m_ball.transform.localPosition = new Vector2(0, 0); m_isStartMove = false; gameObject.transform.position = m_rockerInitPos; if (m_controlObj != null) { if (m_controlObj.GetComponent <Hero01Script>().m_heroState == Hero01Script.HeroState.HeroState_walk) { m_controlObj.GetComponent <Hero01Script>().idle(); } } } } else if (Input.GetMouseButton(0)) { if (m_isStartMove) { if (UtilsScripts.TwoPointDistance2D(touchPos, m_bg.transform.position) <= m_bgRadius) { m_ball.transform.position = touchPos; } else { float k = (touchPos.y - m_bg.transform.position.y) / (touchPos.x - m_bg.transform.position.x); float x = Mathf.Cos(Mathf.Atan(k)) * m_bgRadius; float y = Mathf.Sin(Mathf.Atan(k)) * m_bgRadius; x = Mathf.Abs(x); y = Mathf.Abs(y); if ((touchPos.x < m_bg.transform.position.x) && (touchPos.y > m_bg.transform.position.y)) { x = -x; } else if ((touchPos.x < m_bg.transform.position.x) && (touchPos.y < m_bg.transform.position.y)) { x = -x; y = -y; } else if ((touchPos.x > m_bg.transform.position.x) && (touchPos.y < m_bg.transform.position.y)) { y = -y; } m_ball.transform.localPosition = new Vector2(x, y); } { float angle = getRockerAngle(); if (m_controlObj != null) { m_controlObj.GetComponent <Hero01Script>().walk(angle); } } } } return; } //-------------------------------------------------------------------------------------------------------------------------------- else if (Input.touchCount > 0) { Vector2 ballPos = m_ball.transform.position; int touchId = -1; { for (int i = 0; i < Input.touchCount; i++) { if (UtilsScripts.TwoPointDistance2D(Input.GetTouch(i).position, m_bg.transform.position) <= m_bgRadius) { touchId = i; break; } else if (UtilsScripts.TwoPointDistance2D(Input.GetTouch(i).position, new Vector2(0, 0)) <= 500) { touchId = i; break; } } } if (touchId == -1) { return; } Vector2 touchPos = Input.GetTouch(touchId).position; if (Input.GetTouch(touchId).phase == TouchPhase.Began) { if (UtilsScripts.TwoPointDistance2D(touchPos, m_bg.transform.position) <= m_bgRadius) { m_isStartMove = true; m_ball.transform.position = touchPos; } else if (UtilsScripts.TwoPointDistance2D(touchPos, new Vector2(0, 0)) <= 500) { gameObject.transform.position = touchPos; m_isStartMove = true; m_ball.transform.position = touchPos; } } else if ((Input.GetTouch(touchId).phase == TouchPhase.Stationary) || (Input.GetTouch(touchId).phase == TouchPhase.Moved)) { if (m_isStartMove) { if (UtilsScripts.TwoPointDistance2D(touchPos, m_bg.transform.position) <= m_bgRadius) { m_ball.transform.position = touchPos; } else { float k = (touchPos.y - m_bg.transform.position.y) / (touchPos.x - m_bg.transform.position.x); float x = Mathf.Cos(Mathf.Atan(k)) * m_bgRadius; float y = Mathf.Sin(Mathf.Atan(k)) * m_bgRadius; x = Mathf.Abs(x); y = Mathf.Abs(y); if ((touchPos.x < m_bg.transform.position.x) && (touchPos.y > m_bg.transform.position.y)) { x = -x; } else if ((touchPos.x < m_bg.transform.position.x) && (touchPos.y < m_bg.transform.position.y)) { x = -x; y = -y; } else if ((touchPos.x > m_bg.transform.position.x) && (touchPos.y < m_bg.transform.position.y)) { y = -y; } m_ball.transform.localPosition = new Vector2(x, y); } { float angle = getRockerAngle(); if (m_controlObj != null) { m_controlObj.GetComponent <Hero01Script>().walk(angle); } } } } else if (Input.GetTouch(touchId).phase == TouchPhase.Ended) { if (m_isStartMove) { m_ball.transform.localPosition = new Vector2(0, 0); m_isStartMove = false; gameObject.transform.position = m_rockerInitPos; if (m_controlObj != null) { if (m_controlObj.GetComponent <Hero01Script>().m_heroState == Hero01Script.HeroState.HeroState_walk) { m_controlObj.GetComponent <Hero01Script>().idle(); } } } } } }