public bool LineOfSight(Vector3 position, PlayerEntity target, bool checkposition) { if ((position - target.position.Value).magnitude < 0.5) { return(true); } var bonePosition = target.bones.Head; var offset = Vector3.zero; offset = target.RootGo().transform.InverseTransformPoint(target.cameraObj.FPCamera .transform.position); if (checkposition) { if (!NavMesh.SamplePosition(position, out m_NavMeshHit, 2f, NavMesh.AllAreas)) { return(false); } } else { Physics.Linecast(position + offset, bonePosition.position, out hit, UnityLayerManager.GetLayerMask(EUnityLayerName.Player)); } return(true); }
public override UnitPosition Select(IEventArgs args) { UnitPosition up = pos.Select(args); Vector3 position = UnityPositionUtil.ToVector3(up); RaycastHit hitInfo; if (SingletonManager.Get <MapConfigManager>().InWater(position)) { Ray ray = new Ray(position, Vector3.up); bool hit = Physics.Raycast(ray, out hitInfo, 10000, UnityLayerManager.GetLayerMask(EUnityLayerName.WaterTrigger)); if (hit) { return(UnityPositionUtil.FromVector(hitInfo.point - new Vector3(0, waterDelta, 0))); } } else { Ray ray = new Ray(position, Vector3.down); bool hit = Physics.Raycast(ray, out hitInfo, 10000, UnityLayers.SceneCollidableLayerMask | UnityLayerManager.GetLayerMask(EUnityLayerName.WaterTrigger)); if (hit) { if (hitInfo.collider.transform.gameObject.layer == UnityLayerManager.GetLayerIndex(EUnityLayerName.WaterTrigger)) { return(UnityPositionUtil.FromVector(hitInfo.point - new Vector3(0, waterDelta, 0))); } else { return(UnityPositionUtil.FromVector(hitInfo.point)); } } } return(up); }
private bool HasObstacle(Vector3 colPosition, Vector3 bombPosition, Func <Transform, bool> exclude = null) { RaycastHit hitInfo; if (null == exclude) { Debug.DrawLine(bombPosition, colPosition, Color.red, 10f); if (Physics.Linecast(bombPosition, colPosition, out hitInfo, UnityLayerManager.GetLayerMask(EUnityLayerName.Default))) { return(true); } return(false); } var dir = colPosition - bombPosition; var obstacles = Physics.RaycastAll(bombPosition, dir, dir.magnitude, UnityLayerManager.GetLayerMask(EUnityLayerName.Default)); foreach (var obstacle in obstacles) { if (!exclude(obstacle.transform)) { return(true); } } return(false); }
protected override void InternalExecute() { var player = _contexts.player.flagSelfEntity; var controller = player.WeaponController(); if (!player.gamePlay.IsLifeState(EPlayerLifeState.Alive) || !player.gamePlay.CanAutoPick()) { return; } Collider[] colliders = Physics.OverlapCapsule(player.position.Value, player.bones.Head.position, 0.4f, UnityLayerManager.GetLayerMask(EUnityLayerName.UserInputRaycast)); foreach (Collider collider in colliders) { var rcTar = collider.transform.GetComponentInParent <RayCastTarget>(); if (rcTar == null) { continue; } if (SingletonManager.Get <GameModeConfigManager>().GetBagTypeById(_contexts.session.commonSession.RoomInfo.ModeId) == EBagType.Chicken) { var target = _contexts.sceneObject.GetEntityWithEntityKey(new EntityKey(rcTar.IdList[1], (short)EEntityType.SceneObject)); if (target.simpleItem.Category == (int)ECategory.Weapon && player.WeaponController().FilterAutoPickup(target.simpleItem.Id)) { var model = target.hasUnityObject ? target.unityObject.UnityObject : target.multiUnityObject.FirstAsset; if (CommonObjectCastUtil.HasObstacleBetweenPlayerAndItem(player, target.position.Value, model)) { continue; } SimpleProto pickUp = FreePool.Allocate(); pickUp.Key = FreeMessageConstant.PickUpItem; pickUp.Ins.Add(rcTar.IdList[1]); pickUp.Ins.Add(target.simpleItem.Category); pickUp.Ins.Add(target.simpleItem.Id); pickUp.Ins.Add(1); player.network.NetworkChannel.SendReliable((int)EClient2ServerMessage.FreeEvent, pickUp); } } else { _userCmdGenerator.SetUserCmd((userCmd) => { if (!userCmd.AutoPickUpEquip.Contains(rcTar.IdList[1])) { userCmd.AutoPickUpEquip.Add(rcTar.IdList[1]); } }); } } if (controller.AutoThrowing.HasValue && controller.AutoThrowing.Value) { if (null != _userCmdGenerator && controller.RelatedThrowAction.IsReady == true) { _userCmdGenerator.SetUserCmd((userCmd) => { userCmd.IsThrowing = true; }); controller.AutoThrowing = false; } } }
private void DoExlopsionDamageToNeighboringObjects(VehicleEntity vehicle) { var explosionCenter = vehicle.GetExplosionCenter(); var layerMask = UnityLayerManager.GetLayerMask(EUnityLayerName.Player) | UnityLayerManager.GetLayerMask(EUnityLayerName.Vehicle); Collider[] colliders = Physics.OverlapSphere(explosionCenter, MaxOverlapRadius, layerMask); DoExplosionDamageToColliders(vehicle, colliders, explosionCenter); }
private static Vector3 GetNewBirthPositionOnOverlap(GameObject go) { var position = go.transform.position; if (SharedConfig.IsOffline || SharedConfig.IsServer) { var rigidBody = go.GetComponent <Rigidbody>(); int tryCount = 0; const float checkRadius = 5.0f; while (true) { var colliders = Physics.OverlapSphere(position, checkRadius, UnityLayerManager.GetLayerMask(EUnityLayerName.Vehicle)); if (colliders.Length == 0) { break; } var colliderCount = colliders.Length; var noOtherVehicle = true; for (int i = 0; i < colliderCount; ++i) { if (colliders[i].attachedRigidbody != rigidBody) { noOtherVehicle = false; break; } } if (noOtherVehicle) { break; } _logger.WarnFormat("There is a vehicle in birth position {0}", position); tryCount++; var angle = UnityEngine.Random.Range(0, 2 * (float)Math.PI); var dir = new Vector3(Mathf.Sin(angle), 0.0f, Mathf.Cos(angle)); position += checkRadius * tryCount * dir; const float liftHeight = 1000.0f; position.y += liftHeight; RaycastHit hit; if (Physics.Raycast(position, -Vector3.up, out hit, 2 * liftHeight, UnityLayers.SceneCollidableLayerMask)) { position = hit.point + new Vector3(0, 2.0f, 0); } } } return(position); }
public static bool HasObstacleBetweenPlayerAndItem(PlayerEntity player, Vector3 targetCenter, GameObject item) { // Logger.Debug("HasObstacleBeteenPlayerAndItem"); if (null == player) { Logger.Error("player is null"); return(true); } _castCount = 0; var startPoint = player.cameraStateOutputNew.FinalArchorPosition; int mask = ~(UnityLayerManager.GetLayerMask(EUnityLayerName.UserInputRaycast) | UnityLayerManager.GetLayerMask(EUnityLayerName.Player) | UnityLayerManager.GetLayerMask(EUnityLayerName.Vehicle) | UnityLayerManager.GetLayerMask(EUnityLayerName.NoCollisionWithBullet) | UnityLayerManager.GetLayerMask(EUnityLayerName.NoCollisionWithEntity)); #if DEBUG_OBSTACLE DebugDraw.DebugArrow(startPoint, targetCenter - startPoint); if (null == _startGo) { _startGo = GameObject.CreatePrimitive(PrimitiveType.Cube); _startGo.GetComponent <Collider>().enabled = false; _targetGo = GameObject.CreatePrimitive(PrimitiveType.Cube); _targetGo.GetComponent <Collider>().enabled = false; _startGo.transform.localScale = Vector3.one * 0.1f; _targetGo.transform.localScale = Vector3.one * 0.1f; } _startGo.transform.position = startPoint; _targetGo.transform.position = targetCenter; #endif var dir = startPoint - targetCenter; var ray = new Ray(targetCenter, dir.normalized); var inRangeOffset = -0.01f; var hits = Physics.RaycastAll(ray, dir.magnitude - inRangeOffset, mask); foreach (var hit in hits) { Logger.DebugFormat("hit {0}", hit.collider.name); if (Ignore(hit, player, item)) { continue; } else { OnObstacle(true); // Logger.Debug("onObstacle"); return(true); } } // Logger.Debug("onObstacle"); OnObstacle(false); return(false); }
public override void DoAction(IEventArgs args) { UnitPosition up = pos.Select(args); float realRadius = FreeUtil.ReplaceFloat(radius, args); float realDamage = FreeUtil.ReplaceFloat(damage, args); EUIDeadType realType = (EUIDeadType)FreeUtil.ReplaceInt(type, args); if (damagePara == null) { damagePara = new FloatPara("damage", realDamage); disPara = new FloatPara("dis", 0f); typePara = new StringPara("type", FreeUtil.ReplaceVar(type, args)); } if (up != null) { var bombPos = new Vector3(up.GetX(), up.GetY(), up.GetZ()); var colliders = Physics.OverlapSphere(bombPos, realRadius, UnityLayerManager.GetLayerMask(EUnityLayerName.Player) | UnityLayerManager.GetLayerMask(EUnityLayerName.UserInputRaycast) | UnityLayerManager.GetLayerMask(EUnityLayerName.Vehicle) | UnityLayerManager.GetLayerMask(EUnityLayerName.Glass)); foreach (var collider in colliders) { float trueDamage = CalculateBombDamage(collider.transform, bombPos, realDamage, realType); if (Logger.IsDebugEnabled) { Logger.DebugFormat("Process {0}", collider.name); } if (collider.gameObject.layer == UnityLayerManager.GetLayerIndex(EUnityLayerName.Player)) { HandlePlayer(collider, args, args.GameContext, trueDamage, bombPos); } if (collider.gameObject.layer == UnityLayerManager.GetLayerIndex(EUnityLayerName.UserInputRaycast)) { HandleFracturedObjects(collider.transform, bombPos, trueDamage); } if (collider.gameObject.layer == UnityLayerManager.GetLayerIndex(EUnityLayerName.Vehicle)) { HandleVehicle(collider.transform, trueDamage); } if (collider.gameObject.layer == UnityLayerManager.GetLayerIndex(EUnityLayerName.Glass)) { HandleGlass(collider.transform); } } } }
public virtual Camera CreateFxCamera(Camera mainCamera) { mainCamera.cullingMask ^= UnityLayerManager.GetLayerMask(EUnityLayerName.CameraFx); var fxCamera = new GameObject("FxCamera").AddComponent <Camera>(); fxCamera.transform.SetParent(mainCamera.transform); fxCamera.transform.localPosition = Vector3.zero; fxCamera.transform.localRotation = Quaternion.identity; fxCamera.transform.localScale = Vector3.one; fxCamera.cullingMask = UnityLayerManager.GetLayerMask(EUnityLayerName.CameraFx); fxCamera.nearClipPlane = 0.01f; fxCamera.depth = mainCamera.depth + 2; fxCamera.clearFlags = CameraClearFlags.Depth; fxCamera.renderingPath = RenderingPath.Forward; fxCamera.useOcclusionCulling = false; return(fxCamera); }
public void OnGamePlay() { _removeEntityList.Clear(); foreach (SceneObjectEntity sceneObjectEntiy in _throwingSceneObjectGroup) { if (sceneObjectEntiy.throwing.Time < 1) { sceneObjectEntiy.throwing.Time = _currentTime.CurrentTime; continue; } var interval = _currentTime.CurrentTime - sceneObjectEntiy.throwing.Time; var secondInterval = interval * 0.001f; sceneObjectEntiy.throwing.Time = _currentTime.CurrentTime; sceneObjectEntiy.ReplaceFlagImmutability(_currentTime.CurrentTime); var velocity = sceneObjectEntiy.throwing.Velocity * secondInterval; velocity.y += _gameConfig.WeaponDropVSpeed * secondInterval; var lastPos = sceneObjectEntiy.position.Value; var newPos = lastPos + velocity; sceneObjectEntiy.throwing.Velocity = velocity; Debug.LogFormat("Velotcity is {0} interval {1} offset {2} pos {3}", velocity, interval, (newPos - lastPos).ToStringExt(), sceneObjectEntiy.position.Value.ToStringExt()); RaycastHit hitInfo; if (Physics.Linecast(lastPos, newPos, out hitInfo, UnityLayerManager.GetLayerMask(EUnityLayerName.Default) | UnityLayerManager.GetLayerMask(EUnityLayerName.Terrain))) { sceneObjectEntiy.position.Value = hitInfo.point; Debug.LogFormat("hit {0} in {1}", hitInfo.transform.name, hitInfo.point); _removeEntityList.Add(sceneObjectEntiy); } else { sceneObjectEntiy.position.Value = newPos; } } foreach (var sceneObjectentity in _removeEntityList) { if (null != sceneObjectentity && sceneObjectentity.isEnabled && sceneObjectentity.hasThrowing) { sceneObjectentity.RemoveThrowing(); } } }
public void OnEntityInit() { foreach (var player in _iGroup.GetEntities()) { var mainCamera = Camera.main; if (null == mainCamera) { if (Time.time - lastLogTime > 2000) { Logger.Error("no init main camera in scene"); lastLogTime = Time.time; } return; } try { ICameraFactory cameraFactory = new SingleCameraFactory(); mainCamera.transform.localScale = Vector3.one; mainCamera.cullingMask &= ~UnityLayerManager.GetLayerMask(EUnityLayerName.UI); mainCamera.cullingMask &= ~UnityLayerManager.GetLayerMask(EUnityLayerName.UIParticle); mainCamera.cullingMask &= ~UnityLayerManager.GetLayerMask(EUnityLayerName.RenderUIParticle); var fpCamera = cameraFactory.CreateFpCamera(mainCamera); var fxCamera = cameraFactory.CreateFxCamera(mainCamera); player.AddCameraObj(); player.AddCameraFx(); player.cameraObj.EffectCamera = fxCamera; player.cameraObj.FPCamera = fpCamera; player.cameraObj.MainCamera = mainCamera; } catch { Logger.Error("MainCamera is null ??? !!!"); } } }
private static void ResolveOverlapWithPlayer(Contexts contexts, PlayerEntity player) { Vector3 point1, point2; float radius; var controller = player.characterContoller.Value; if (controller.GetCurrentControllerType() != CharacterControllerType.UnityCharacterController) { return; } var characterController = controller.RealValue as CharacterController; if (characterController == null) { return; } var height = controller.height; radius = controller.radius; var center = controller.center; PhysicsCastHelper.GetCapsule(controller.transform.position, controller.transform.rotation, height, radius, center, controller.direction, out point1, out point2); var colliders = PhysicsCastHelper.CollidersArray; var hits = PhysicsCastHelper.CapsuleOverlap( point1, point2, radius, colliders, UnityLayerManager.GetLayerMask(EUnityLayerName.Player), QueryTriggerInteraction.Ignore, collider => { if (collider == controller.GetCollider() || !(collider is CharacterController)) { return(true); } return(false); }); // calculate penetration for (int i = 0; i < hits; ++i) { Vector3 resolutionDirection = Vector3.up; float resolutionDistance = 0f; Transform overlappedTransform = colliders[i].GetComponent <Transform>(); if (Physics.ComputePenetration(controller.GetCollider(), controller.transform.position, controller.transform.rotation, colliders[i], overlappedTransform.position, overlappedTransform.rotation, out resolutionDirection, out resolutionDistance )) { characterController.Move(resolutionDirection * resolutionDistance); //_logger.InfoFormat("resolutionDirection:{0}, resolutionDistance:{1}, hitCollider:{2}", resolutionDirection, resolutionDistance, overlappedTransform.name); //DebugDraw.DebugArrow(controller.transform.position, resolutionDirection, Color.magenta, 20f, false); } } }
public static bool GetRideOffPosition(PlayerEntity playerEntity, VehicleEntity vehicleEntity, Vector3 direction, out Vector3 resolvedPosition, float liftHeight = 0.01f, float displacement = 0.05f) { var character = playerEntity.RootGo(); const float sweepDistance = 5.0f; direction = -direction; var p = character.transform.position - direction * sweepDistance; Vector3 p1, p2; float radius; PlayerEntityUtility.GetCapsule(playerEntity, p, out p1, out p2, out radius); vehicleEntity.SetLayer(UnityLayerManager.GetLayerIndex(EUnityLayerName.User)); resolvedPosition = character.transform.position; var hit = new RaycastHit(); var hitDist = 2.0f; var lowOffset = new Vector3(0, -0.5f, 0); if (Physics.CapsuleCast(p1, p2, radius, direction, out hit, sweepDistance, UnityLayerManager.GetLayerMask(EUnityLayerName.User)) || //the seat position may be higher than the vehicle's height, then low the position to get collided position Physics.CapsuleCast(p1 + lowOffset, p2 + lowOffset, radius, direction, out hit, sweepDistance, UnityLayerManager.GetLayerMask(EUnityLayerName.User))) { hitDist = hit.distance; } { var distance = (hitDist - displacement) * direction; var colliders = Physics.OverlapCapsule(p1 + distance, p2 + distance, radius); if (colliders.Length > 0) { vehicleEntity.SetLayer(UnityLayerManager.GetLayerIndex(EUnityLayerName.Vehicle)); return(false); } var disp = direction * (hitDist - displacement - sweepDistance); var position = resolvedPosition; position += disp; if (liftHeight > 0.0f) { RaycastHit upHit;; Physics.CapsuleCast(p1 + disp, p2 + disp, radius, Vector3.up, out upHit, liftHeight); position += upHit.distance * Vector3.up; } var dist = resolvedPosition - position; var ray = new Ray(position, dist.normalized); if (Physics.Raycast(ray, out hit, dist.magnitude, UnityLayers.AllCollidableLayerMask)) { vehicleEntity.SetLayer(UnityLayerManager.GetLayerIndex(EUnityLayerName.Vehicle)); return(false); } resolvedPosition = position; } vehicleEntity.SetLayer(UnityLayerManager.GetLayerIndex(EUnityLayerName.Vehicle)); return(true); }
private static void ResolveOverlapWithPlayer(Contexts contexts, PlayerEntity player) { Vector3 point1, point2; float radius; var controller = player.characterContoller.Value; if (controller.GetCurrentControllerType() != CharacterControllerType.UnityCharacterController) { return; } var characterController = controller.RealValue as CharacterController; if (characterController == null) { return; } var height = controller.height; radius = controller.radius; var center = controller.center; PhysicsCastHelper.GetCapsule(controller.transform.position, controller.transform.rotation, height, radius, center, controller.direction, out point1, out point2); var colliders = PhysicsCastHelper.CollidersArray; var hits = PhysicsCastHelper.CapsuleOverlap( point1, point2, radius, colliders, UnityLayerManager.GetLayerMask(EUnityLayerName.Player), QueryTriggerInteraction.Ignore, collider => { //和角色做分离,趴下不需要,自带分离 if (collider == controller.GetCollider() || !(collider is CapsuleCollider && (collider as CapsuleCollider).direction == 1)) { return(true); } return(false); }); int MaxStep = 1; // calculate penetration for (int i = 0; i < hits; ++i) { Vector3 resolutionDirection = Vector3.up; float resolutionDistance = 0f; Transform overlappedTransform = colliders[i].GetComponent <Transform>(); for (int j = 0; j < MaxStep; ++j) { if (Physics.ComputePenetration(characterController, characterController.transform.position, characterController.transform.rotation, colliders[i], colliders[i].transform.position, colliders[i].transform.rotation, out resolutionDirection, out resolutionDistance )) { characterController.Move(new Vector3(0, -0.01f, 0)); _logger.DebugFormat( "ResolveOverlapWithPlayer ResolveOverlapWithPlayer resolutionDirection:{0}, resolutionDistance:{1}, hitCollider:{2}", resolutionDirection.ToStringExt(), resolutionDistance, overlappedTransform.name); } else { break; } } } }
private void GrenadeDamageHandler(ThrowingEntity throwing) { PlayerEntity sourcePlayer = null; if (throwing.hasOwnerId) { sourcePlayer = _contexts.player.GetEntityWithEntityKey(throwing.ownerId.Value); } Vector3 hitPoint; foreach (PlayerEntity player in _players) { if (player.hasPlayerMask && player.playerMask.SelfMask == (int)EPlayerMask.Invincible) { continue; } float dis = Vector3.Distance(throwing.position.Value, player.position.Value); if (dis < throwing.throwingData.ThrowConfig.DamageRadius && ((!throwing.throwingData.IsFly && throwing.ownerId.Value == player.entityKey.Value) || !CommonMathUtil.Raycast(throwing.position.Value, player.position.Value, dis, _layerMask, out hitPoint) || !CommonMathUtil.Raycast(throwing.position.Value, player.bones.Head.position, dis, _layerMask, out hitPoint))) { float damage = (1 - dis / throwing.throwingData.ThrowConfig.DamageRadius) * throwing.throwingData.ThrowConfig.BaseDamage; _throwingHitHandler.OnPlayerDamage(_contexts, sourcePlayer, player, new PlayerDamageInfo(damage, (int)EUIDeadType.Weapon, (int)EBodyPart.None, GetWeaponIdBySubType((EWeaponSubType)throwing.throwingData.WeaponSubType), false, false, false, player.position.Value, player.position.Value - throwing.position.Value)); } } foreach (VehicleEntity vehicle in _vehicles) { float dis = Vector3.Distance(throwing.position.Value, vehicle.position.Value); if (dis < throwing.throwingData.ThrowConfig.DamageRadius && !CommonMathUtil.Raycast(throwing.position.Value, vehicle.position.Value, dis, _layerMask, out hitPoint)) { float damage = (1 - dis / throwing.throwingData.ThrowConfig.DamageRadius) * throwing.throwingData.ThrowConfig.BaseDamage; _throwingHitHandler.OnVehicleDamage(vehicle, damage); } } var colliders = Physics.OverlapSphere(throwing.position.Value, throwing.throwingData.ThrowConfig.DamageRadius, UnityLayerManager.GetLayerMask(EUnityLayerName.UserInputRaycast) | UnityLayerManager.GetLayerMask(EUnityLayerName.Glass)); foreach (var collider in colliders) { CreateMapObjWhenBomb(collider, sourcePlayer); var distance = Vector3.Distance(collider.transform.position, throwing.position.Value); float trueDamage = distance > throwing.throwingData.ThrowConfig.DamageRadius ? 0f : Mathf.Max(0f, throwing.throwingData.ThrowConfig.BaseDamage * (1 - distance / throwing.throwingData.ThrowConfig.DamageRadius)); if (collider.gameObject.layer == UnityLayerManager.GetLayerIndex(EUnityLayerName.UserInputRaycast)) { var parent = collider.transform; while (null != parent) { var fractured = parent.GetComponent <FracturedObject>(); if (null != fractured) { if (!HasObstacle(collider.transform.position, throwing.position.Value, (obstacleTrans) => { var obstacleParent = obstacleTrans.parent; while (null != obstacleParent) { if (obstacleParent == fractured.transform) { return(true); } obstacleParent = obstacleParent.parent; } return(false); })) { fractured.Explode(Vector3.zero, trueDamage); } break; } parent = parent.parent; } } if (collider.gameObject.layer == UnityLayerManager.GetLayerIndex(EUnityLayerName.Glass)) { var parent = collider.transform; while (null != parent) { var fractured = parent.GetComponent <FracturedGlassyChunk>(); if (null != fractured) { fractured.MakeBroken(); } parent = parent.parent; } } } }