void Update() { m_lastJumpTimer += Time.deltaTime; if (PlayerInput.FireKey.GetKeyDown()) { //SoundManager.PlayClipAtPoint("WeaponShot", transform.position, 0.5f); RayHitInfo hitInfo = RaycastHelper.Raycast(Camera.main.transform.position, Camera.main.transform.position + Camera.main.transform.forward * 300); if (hitInfo.rayHit) { int hitPlayerId = 0; if (hitInfo.hitType == RayFlags.Player) { hitPlayerId = hitInfo.raycastHit.collider.GetComponent <Player>().m_playerId; } MessagePlayerShotBullet message = new MessagePlayerShotBullet(); message.playerId = gameObject.GetComponent <Player>().m_playerId; message.hitPlayerId = hitPlayerId; message.damage = 10; message.hitPosition = hitInfo.point; message.hitDirection = (transform.position - hitInfo.point); MultiplayerManager.instance.SendNetworkMessage(message, SendTo.All); } } m_hookCooldown -= Time.deltaTime; m_hookCooldown = Math.Max(0, m_hookCooldown); if (PlayerInput.SecFireKey.GetKeyDown() && m_hookCooldown <= 0) { Vector3 startPointShot = Camera.main.transform.position; Vector3 endPointShot = Camera.main.transform.position + Camera.main.transform.forward * 100; m_rayCastInfo = RaycastHelper.Raycast(startPointShot, endPointShot); m_hookHitPosition = m_rayCastInfo.point; if (m_rayCastInfo.rayHit) { m_hookCooldown = HOOK_COOLDOWN; m_hookHit = true; m_hookLineRender.SetPosition(0, transform.position); m_hookLineRender.SetPosition(1, m_hookHitPosition); m_hookLineRender.enabled = true; SendPlayerHookUsed(m_hookHitPosition); } } if (PlayerInput.SecFireKey.GetKey() && m_hookHit) { if (m_rayCastInfo.hitType == RayFlags.Citizen && m_rayCastInfo.index != 0) { var cInstance = Singleton <CitizenManager> .instance; var citizenInstace = cInstance.m_instances.m_buffer[m_rayCastInfo.index]; var citizenInfo = citizenInstace.Info; Vector3 citizenPosition; Quaternion citizenRotation; citizenInstace.GetSmoothPosition((ushort)m_rayCastInfo.index, out citizenPosition, out citizenRotation); m_hookHitPosition = citizenPosition; m_hookLineRender.SetPosition(1, m_hookHitPosition); SendPlayerHookUsed(m_hookHitPosition); } else { if (m_rayCastInfo.hitType == RayFlags.Vehicle && m_rayCastInfo.index != 0) { var vInstance = Singleton <VehicleManager> .instance; var citizenInstace = vInstance.m_vehicles.m_buffer[m_rayCastInfo.index]; var citizenInfo = citizenInstace.Info; Vector3 citizenPosition; Quaternion citizenRotation; citizenInstace.GetSmoothPosition((ushort)m_rayCastInfo.index, out citizenPosition, out citizenRotation); m_hookHitPosition = citizenPosition; m_hookLineRender.SetPosition(1, m_hookHitPosition); SendPlayerHookUsed(m_hookHitPosition); } } m_hookLineRender.SetPosition(0, transform.position); Vector3 dir = (m_hookHitPosition - transform.position).normalized; float hookForce = Math.Min(Vector3.Distance(transform.position, m_hookHitPosition) * 15, HOOK_FORCE); m_velocity = dir * hookForce; m_rigidBody.velocity = m_velocity; } if (PlayerInput.SecFireKey.GetKeyUp() && m_hookHit) { OnHookReleased(); } }
public static RayHitInfo Raycast(Vector3 startPos, Vector3 endPos) { var rayHitInfo = new RayHitInfo(); Segment3 segment = new Segment3(startPos, endPos); Vector3 direction = (endPos - startPos).normalized; float length = Vector3.Distance(startPos, endPos); float closestRayHitDistance = float.MaxValue; Vector3 tempHitPos; ushort a; ushort b; RaycastHit hit; Ray ray = new Ray(startPos, endPos - startPos); if (Physics.Raycast(ray, out hit)) { closestRayHitDistance = Vector3.SqrMagnitude(startPos - hit.point); rayHitInfo.rayHit = true; rayHitInfo.point = hit.point; rayHitInfo.raycastHit = hit; if (hit.collider.tag == "Player") { rayHitInfo.hitType = RayFlags.Player; } else { rayHitInfo.hitType = RayFlags.Physics; } } if (Singleton<NetManager>.instance.RayCast(segment, 0f, ItemClass.Service.None, ItemClass.Service.None, ItemClass.SubService.None, ItemClass.SubService.None, ItemClass.Layer.Default, ItemClass.Layer.None, NetNode.Flags.OnGround, NetSegment.Flags.None, out tempHitPos, out a, out b)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Road; rayHitInfo.point = tempHitPos; } } if (Singleton<BuildingManager>.instance.RayCast(segment, ItemClass.Service.None, ItemClass.SubService.None, ItemClass.Layer.Default, Building.Flags.None, out tempHitPos, out a)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Building; rayHitInfo.point = tempHitPos; } } if (Singleton<PropManager>.instance.RayCast(segment, ItemClass.Service.None, ItemClass.SubService.None, ItemClass.Layer.Default, PropInstance.Flags.None, out tempHitPos, out a)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Prop; rayHitInfo.point = tempHitPos; } } if (Singleton<TerrainManager>.instance.RayCast(segment, out tempHitPos)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Terrain; rayHitInfo.point = tempHitPos; } } uint c; if (Singleton<TreeManager>.instance.RayCast(segment, ItemClass.Service.None, ItemClass.SubService.None, ItemClass.Layer.Default, TreeInstance.Flags.None, out tempHitPos,out c)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Tree; rayHitInfo.point = tempHitPos; } } if (Singleton<CitizenManager>.instance.RayCast(segment, CitizenInstance.Flags.None, out tempHitPos, out a)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Citizen; rayHitInfo.point = tempHitPos; rayHitInfo.index = a; } } if (Singleton<VehicleManager>.instance.RayCast(segment, Vehicle.Flags.None, VehicleParked.Flags.All, out tempHitPos, out a, out b)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Vehicle; rayHitInfo.point = tempHitPos; rayHitInfo.index = a != 0 ? a : b; } } if (Singleton<VehicleManager>.instance.RayCast(segment, Vehicle.Flags.All, VehicleParked.Flags.None, out tempHitPos, out a, out b)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.VehicleParked; rayHitInfo.point = tempHitPos; rayHitInfo.index = a != 0 ? a : b; } } return rayHitInfo; }
public static RayHitInfo Raycast(Vector3 startPos, Vector3 endPos) { var rayHitInfo = new RayHitInfo(); Segment3 segment = new Segment3(startPos, endPos); Vector3 direction = (endPos - startPos).normalized; float length = Vector3.Distance(startPos, endPos); float closestRayHitDistance = float.MaxValue; Vector3 tempHitPos; ushort a; ushort b; RaycastHit hit; Ray ray = new Ray(startPos, endPos - startPos); if (Physics.Raycast(ray, out hit)) { closestRayHitDistance = Vector3.SqrMagnitude(startPos - hit.point); rayHitInfo.rayHit = true; rayHitInfo.point = hit.point; rayHitInfo.raycastHit = hit; if (hit.collider.tag == "Player") { rayHitInfo.hitType = RayFlags.Player; } else { rayHitInfo.hitType = RayFlags.Physics; } } if (Singleton <NetManager> .instance.RayCast(segment, 0f, ItemClass.Service.None, ItemClass.Service.None, ItemClass.SubService.None, ItemClass.SubService.None, ItemClass.Layer.Default, ItemClass.Layer.None, NetNode.Flags.OnGround, NetSegment.Flags.None, out tempHitPos, out a, out b)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Road; rayHitInfo.point = tempHitPos; } } if (Singleton <BuildingManager> .instance.RayCast(segment, ItemClass.Service.None, ItemClass.SubService.None, ItemClass.Layer.Default, Building.Flags.None, out tempHitPos, out a)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Building; rayHitInfo.point = tempHitPos; } } if (Singleton <PropManager> .instance.RayCast(segment, ItemClass.Service.None, ItemClass.SubService.None, ItemClass.Layer.Default, PropInstance.Flags.None, out tempHitPos, out a)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Prop; rayHitInfo.point = tempHitPos; } } if (Singleton <TerrainManager> .instance.RayCast(segment, out tempHitPos)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Terrain; rayHitInfo.point = tempHitPos; } } uint c; if (Singleton <TreeManager> .instance.RayCast(segment, ItemClass.Service.None, ItemClass.SubService.None, ItemClass.Layer.Default, TreeInstance.Flags.None, out tempHitPos, out c)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Tree; rayHitInfo.point = tempHitPos; } } if (Singleton <CitizenManager> .instance.RayCast(segment, CitizenInstance.Flags.None, out tempHitPos, out a)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Citizen; rayHitInfo.point = tempHitPos; rayHitInfo.index = a; } } if (Singleton <VehicleManager> .instance.RayCast(segment, Vehicle.Flags.None, VehicleParked.Flags.All, out tempHitPos, out a, out b)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Vehicle; rayHitInfo.point = tempHitPos; rayHitInfo.index = a != 0 ? a : b; } } if (Singleton <VehicleManager> .instance.RayCast(segment, Vehicle.Flags.All, VehicleParked.Flags.None, out tempHitPos, out a, out b)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.VehicleParked; rayHitInfo.point = tempHitPos; rayHitInfo.index = a != 0 ? a : b; } } return(rayHitInfo); }
void Update() { m_lastJumpTimer += Time.deltaTime; if (PlayerInput.FireKey.GetKeyDown()) { //SoundManager.PlayClipAtPoint("WeaponShot", transform.position, 0.5f); RayHitInfo hitInfo = RaycastHelper.Raycast(Camera.main.transform.position, Camera.main.transform.position + Camera.main.transform.forward * 300); if (hitInfo.rayHit) { int hitPlayerId = 0; if (hitInfo.hitType == RayFlags.Player) { hitPlayerId = hitInfo.raycastHit.collider.GetComponent<Player>().m_playerId; } MessagePlayerShotBullet message = new MessagePlayerShotBullet(); message.playerId = gameObject.GetComponent<Player>().m_playerId; message.hitPlayerId = hitPlayerId; message.damage = 10; message.hitPosition = hitInfo.point; message.hitDirection = (transform.position - hitInfo.point); MultiplayerManager.instance.SendNetworkMessage(message, SendTo.All); } } m_hookCooldown -= Time.deltaTime; m_hookCooldown = Math.Max(0, m_hookCooldown); if (PlayerInput.SecFireKey.GetKeyDown() && m_hookCooldown <= 0) { Vector3 startPointShot = Camera.main.transform.position; Vector3 endPointShot = Camera.main.transform.position + Camera.main.transform.forward * 100; m_rayCastInfo = RaycastHelper.Raycast(startPointShot, endPointShot); m_hookHitPosition = m_rayCastInfo.point; if (m_rayCastInfo.rayHit) { m_hookCooldown = HOOK_COOLDOWN; m_hookHit = true; m_hookLineRender.SetPosition(0, transform.position); m_hookLineRender.SetPosition(1, m_hookHitPosition); m_hookLineRender.enabled = true; SendPlayerHookUsed(m_hookHitPosition); } } if (PlayerInput.SecFireKey.GetKey() && m_hookHit) { if (m_rayCastInfo.hitType == RayFlags.Citizen && m_rayCastInfo.index != 0) { var cInstance = Singleton<CitizenManager>.instance; var citizenInstace = cInstance.m_instances.m_buffer[m_rayCastInfo.index]; var citizenInfo = citizenInstace.Info; Vector3 citizenPosition; Quaternion citizenRotation; citizenInstace.GetSmoothPosition((ushort)m_rayCastInfo.index, out citizenPosition, out citizenRotation); m_hookHitPosition = citizenPosition; m_hookLineRender.SetPosition(1, m_hookHitPosition); SendPlayerHookUsed(m_hookHitPosition); } else { if (m_rayCastInfo.hitType == RayFlags.Vehicle && m_rayCastInfo.index != 0) { var vInstance = Singleton<VehicleManager>.instance; var citizenInstace = vInstance.m_vehicles.m_buffer[m_rayCastInfo.index]; var citizenInfo = citizenInstace.Info; Vector3 citizenPosition; Quaternion citizenRotation; citizenInstace.GetSmoothPosition((ushort)m_rayCastInfo.index, out citizenPosition, out citizenRotation); m_hookHitPosition = citizenPosition; m_hookLineRender.SetPosition(1, m_hookHitPosition); SendPlayerHookUsed(m_hookHitPosition); } } m_hookLineRender.SetPosition(0, transform.position); Vector3 dir = (m_hookHitPosition - transform.position).normalized; float hookForce = Math.Min(Vector3.Distance(transform.position, m_hookHitPosition) * 15, HOOK_FORCE); m_velocity = dir * hookForce; m_rigidBody.velocity = m_velocity; } if (PlayerInput.SecFireKey.GetKeyUp() && m_hookHit) { OnHookReleased(); } }
public static RayHitInfo Raycast(Vector3 startPos, Vector3 endPos) { var rayHitInfo = new RayHitInfo(); Segment3 segment = new Segment3(startPos, endPos); Vector3 direction = (endPos - startPos).normalized; float length = Vector3.Distance(startPos, endPos); float closestRayHitDistance = float.MaxValue; Vector3 tempHitPos; ushort a; ushort b; RaycastHit hit; Ray ray = new Ray(startPos, endPos - startPos); if (Physics.Raycast(ray, out hit)) { closestRayHitDistance = Vector3.SqrMagnitude(startPos - hit.point); rayHitInfo.rayHit = true; rayHitInfo.point = hit.point; rayHitInfo.raycastHit = hit; if (hit.collider.tag == "Player") { rayHitInfo.hitType = RayFlags.Player; } else { rayHitInfo.hitType = RayFlags.Physics; } } if (Singleton <NetManager> .instance.RayCast(null, segment, 0f, false, ItemClass.Service.None, ItemClass.Service.None, ItemClass.SubService.None, ItemClass.SubService.None, ItemClass.Layer.Default, ItemClass.Layer.None, NetNode.Flags.OnGround, NetSegment.Flags.None, out tempHitPos, out a, out b)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Road; rayHitInfo.point = tempHitPos; } } if (Singleton <BuildingManager> .instance.RayCast(segment, ItemClass.Service.None, ItemClass.SubService.None, ItemClass.Layer.Default, Building.Flags.None, out tempHitPos, out a)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Building; rayHitInfo.point = tempHitPos; } } if (Singleton <PropManager> .instance.RayCast(segment, ItemClass.Service.None, ItemClass.SubService.None, ItemClass.Layer.Default, PropInstance.Flags.None, out tempHitPos, out a)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Prop; rayHitInfo.point = tempHitPos; } } if (Singleton <TerrainManager> .instance.RayCast(segment, out tempHitPos)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Terrain; rayHitInfo.point = tempHitPos; } } uint c; if (Singleton <TreeManager> .instance.RayCast(segment, ItemClass.Service.None, ItemClass.SubService.None, ItemClass.Layer.Default, TreeInstance.Flags.None, out tempHitPos, out c)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Tree; rayHitInfo.point = tempHitPos; } } if (Singleton <CitizenManager> .instance.RayCast(segment, CitizenInstance.Flags.None, out tempHitPos, out a)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Citizen; rayHitInfo.point = tempHitPos; rayHitInfo.index = a; } } if (Singleton <VehicleManager> .instance.RayCast(segment, 0, VehicleParked.Flags.All, out tempHitPos, out a, out b)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.Vehicle; rayHitInfo.point = tempHitPos; rayHitInfo.index = a != 0 ? a : b; } } var VehicleFlagsAll = Vehicle.Flags.Created | Vehicle.Flags.Deleted | Vehicle.Flags.Spawned | Vehicle.Flags.Inverted | Vehicle.Flags.TransferToTarget | Vehicle.Flags.TransferToSource | Vehicle.Flags.Emergency1 | Vehicle.Flags.Emergency2 | Vehicle.Flags.WaitingPath | Vehicle.Flags.Stopped | Vehicle.Flags.Leaving | Vehicle.Flags.Arriving | Vehicle.Flags.Reversed | Vehicle.Flags.TakingOff | Vehicle.Flags.Flying | Vehicle.Flags.Landing | Vehicle.Flags.WaitingSpace | Vehicle.Flags.WaitingCargo | Vehicle.Flags.GoingBack | Vehicle.Flags.WaitingTarget | Vehicle.Flags.Importing | Vehicle.Flags.Exporting | Vehicle.Flags.Parking | Vehicle.Flags.CustomName | Vehicle.Flags.OnGravel | Vehicle.Flags.WaitingLoading | Vehicle.Flags.Congestion | Vehicle.Flags.DummyTraffic | Vehicle.Flags.Underground | Vehicle.Flags.Transition | Vehicle.Flags.InsideBuilding | Vehicle.Flags.LeftHandDrive; if (Singleton <VehicleManager> .instance.RayCast(segment, VehicleFlagsAll, VehicleParked.Flags.None, out tempHitPos, out a, out b)) { float distance = Vector3.SqrMagnitude(startPos - tempHitPos); if (distance < closestRayHitDistance) { closestRayHitDistance = distance; rayHitInfo.rayHit = true; rayHitInfo.hitType = RayFlags.VehicleParked; rayHitInfo.point = tempHitPos; rayHitInfo.index = a != 0 ? a : b; } } return(rayHitInfo); }