/// <summary> /// 自动瞄准。 /// </summary> private void Update() { PECommon.Log("自动瞄准" + autoAim); //玩家没有设置自动瞄准 if (!autoAim) { return; } if (isOver) { return; } if (Physics.Raycast(Camera.main.ScreenPointToRay(sightPos), out hit)) { //在人物身上了 if (UnityTools.FindUpParent(hit.collider.transform).tag == "Player") { //Debug.Log("瞄准到角色" + UnityTools.FindUpParent(hit.collider.transform).gameObject.name); return; } } foreach (var item in EnemyGODic) { if (item.Value != null) { Vector3 enemyPosV3 = Camera.main.WorldToScreenPoint(item.Value.transform.position); Vector2 enemyPosV2 = new Vector2(enemyPosV3.x, enemyPosV3.y); float distance = Vector2.Distance(sightPos, enemyPosV2); //PECommon.Log("准星距离敌方:" + distance); //进入自动瞄准范围 if (distance < Constants.SelfAimRange) { if (distance <= Constants.SelfAimStopRange) { return; } //PECommon.Log("进入自动瞄准范围,distance为:" + distance+ "lastDis为:" + lastDis); //找到最近的那个目标 if (distance < lastDis) { //要瞄准的目标的位置 finallyPos = enemyPosV2; lastDis = distance; } } } } if (finallyPos != Vector2.zero) { Vector2 dir = finallyPos - sightPos; //改变摄像机朝向 Camera.main.transform.eulerAngles = new Vector3(Camera.main.transform.eulerAngles.x - (dir.y / 360), Camera.main.transform.eulerAngles.y + (dir.x / 360), Camera.main.transform.eulerAngles.z); //人物动作改变: BattleSys.Instance.battleMgr.UpdateModelAction(); finallyPos = Vector2.zero; lastDis = float.MaxValue; } }
public override void CollisionEnter(Collision collision) { PECommon.Log("碰到物体的名字是:" + collision.collider.transform.name); M_Rigidbody.Sleep(); TimerSvc.Instance.RemoveTake(taskID); taskID = -10; TimerSvc.Instance.RemoveTake(trailTaskID); trailTaskID = -100; TimerSvc.Instance.AddTimeTask((int tid) => { bulletPool.AddObject(gameObject); }, 500); if (collision.collider.GetComponent <BulletMark>() != null) { if (Physics.Raycast(ray, out hit, 1000, ~(1 << 10))) { } collision.collider.GetComponent <BulletMark>().CreateBulletMark(hit); } GameObject targetPlayer = UnityTools.FindUpParent(collision.collider.transform).gameObject; if (targetPlayer.tag == "Player") { Vector3 hitPos = collision.collider.transform.position; MyVector3 hitPosMyV3 = UnityTools.GetMyV3Value(hitPos); PECommon.Log("射中的目标最上层物体的名字:" + UnityTools.FindUpParent(collision.collider.transform).gameObject.name); //---------------------------------- if (targetPlayer.GetComponent <ControllerBase>() == null) { return; } //别射到自己。 RoleType targetPlayerRoleType = targetPlayer.GetComponent <ControllerBase>().roleType; if (casterRoleType == targetPlayerRoleType) { return; } int hurt = 0; switch (collision.collider.gameObject.tag) { case "Arm": hurt = damage / 2; PECommon.Log("造成肢体伤害" + hurt); break; case "Body": hurt = damage; PECommon.Log("造成身体伤害" + hurt); break; case "Head": hurt = damage * 2; PECommon.Log("造成头部伤害" + hurt); break; } //发送网络消息 GameMsg gameMsg = new GameMsg { cmd = (int)CMD.ReqTakeDamage, reqTakeDamage = new ReqTakeDamage { damageID = damageID, casterRoleType = casterRoleType, targetRoleType = targetPlayerRoleType, hurt = hurt, hitPos = hitPosMyV3, }, }; netSvc.SendMsg(gameMsg); //准星变红 if (casterRoleType == BattleSys.Instance.GetCurrentRoleType()) { BattleSys.Instance.SetSignRed(); } } gameObject.SetActive(false); }