public static Vector3 GetFireDirection(Transform playerTransform) { Ray ray; string[] temp = Input.GetJoystickNames(); if (Input.GetJoystickNames().Length > 0) { Vector3 mousePos; Vector3 joystick; joystick.x = Input.GetAxis("CursorHorizontal"); joystick.y = -Input.GetAxis("CursorVertical"); joystick.z = 0f; if (joystick.magnitude > 1f) { joystick.Normalize(); } mousePos.x = Screen.width / 2f + joystick.x * (float)Screen.width * fireZoneRadius; mousePos.y = Screen.height / 2f + joystick.y * (float)Screen.height * fireZoneRadius; mousePos.z = 0f; ray = Camera.main.ScreenPointToRay(mousePos); } else { Vector3 mousePos = Input.mousePosition; mousePos.x -= Screen.width / 2f; mousePos.y -= Screen.height / 2f; mousePos.x /= Screen.width; mousePos.y /= Screen.height; mousePos.z = 0f; if (mousePos.magnitude > fireZoneRadius) { mousePos.Normalize(); mousePos *= fireZoneRadius; } mousePos.x *= Screen.width; mousePos.y *= Screen.height; mousePos.x += Screen.width / 2f; mousePos.y += Screen.height / 2f; ray = Camera.main.ScreenPointToRay(mousePos); } Vector3 playerToRay = ray.origin - playerTransform.position; ray.origin = playerTransform.position; ray.origin += Vector3.Dot(playerTransform.right, playerToRay) * playerTransform.right; ray.origin += Vector3.Dot(playerTransform.up, playerToRay) * playerTransform.up; Vector3 destination = ray.origin + ray.direction * Mathf.Sqrt(UIGame.GetCursorDistance() * UIGame.GetCursorDistance() - (ray.origin - playerTransform.position).sqrMagnitude); return((destination - playerTransform.position).normalized); }
void Update() { if (player.GetComponent <Ship>().IsAlive()) { if (shipRef.GetSelectedTarget() == null) { detectTimer += Time.deltaTime; } else { detectTimer = 0f; } if (saveLastHealth != player.GetComponent <Ship>().GetRelativeHealth()) { StartCoroutine(FlashImpact()); } saveLastHealth = player.GetComponent <Ship>().GetRelativeHealth(); health.GetComponent <Image>().fillAmount = player.GetComponent <Ship>().GetRelativeHealth(); speed.GetComponent <Image>().fillAmount = player.GetComponent <Ship>().Throttle; target = player.GetComponent <Ship>().GetSelectedTarget(); if (target == null || Input.GetButton("PushRight")) { targetImage.SetActive(false); fireHelp.SetActive(false); } else { targetImage.SetActive(true); targetImage.transform.position = Camera.main.WorldToScreenPoint(target.transform.position); targetImage.transform.position -= UITransform.forward * Vector3.Dot(UITransform.forward, targetImage.transform.position - UITransform.position); List <WeaponState> wss = player.GetComponentInChildren <WeaponShootManager>().listWeaponState; float averageSpeed = 0f; Vector3 averagePosition = Vector3.zero; int nbWeapons = 0; bool missile = false; foreach (WeaponState ws in wss) { if (ws.type == WeaponStat.WeaponType.MachineGun) { averageSpeed += ws.speedShoot; averagePosition += ws.transform.position; nbWeapons++; } else { missile = true; } } if (nbWeapons > 0 && WSMRef.GetSelectedTypeTurret() == WeaponStat.WeaponType.MachineGun) { fireHelp.SetActive(true); averageSpeed /= nbWeapons; averagePosition /= nbWeapons; Vector3 futurePos = target.transform.position + target.GetComponent <Unit>().GetSpeed() * target.transform.forward / ((averageSpeed + player.GetComponent <Ship>().Velocity.magnitude) / Vector3.Distance(target.transform.position, averagePosition)); fireHelp.transform.position = Camera.main.WorldToScreenPoint(averagePosition + (futurePos - averagePosition).normalized * UIGame.GetCursorDistance()); } else { fireHelp.SetActive(false); } if (missile) { targetImage.GetComponent <Image>().color = player.GetComponent <Ship>().getLockTimerNormalized() < 1f ? Color.Lerp(Color.cyan, Color.green, player.GetComponent <Ship>().getLockTimerNormalized()) : Color.red; } else { targetImage.GetComponent <Image>().color = Color.cyan; } } killSign.GetComponent <Image>().color = Color.Lerp(Color.red, Color.white, killTimer / 2f); if (killTimer < 2f) { killSign.SetActive(true); killCountText.enabled = true; } else { killSign.SetActive(false); killCountText.enabled = false; } killTimer += Time.deltaTime; if (canon != null) { WeaponState ws = shipRef.GetTurret().GetComponent <WeaponState>(); if (!ws.canFire) { loadTimer.SetActive(true); loadTimer.GetComponent <Text>().text = ((int)(ws.fireRate - ws.timerSinceLastShot)).ToString() + "s"; } else { loadTimer.SetActive(false); } } else { loadTimer.SetActive(false); } } else { health.GetComponent <Image>().fillAmount = 0f; if (enemiesUI != null) { foreach (GameObject go in enemiesUI) { go.SetActive(false); } } if (alliesUI != null) { foreach (GameObject go in alliesUI) { go.SetActive(false); } } if (GameObject.Find("PlayerUI") != null) { GameObject.Find("PlayerUI").SetActive(false); } } if (changeMissileTimer < 0.5f) { changeMissile.enabled = true; changeMissileTimer += Time.deltaTime; } else { changeMissile.enabled = false; } timer += Time.deltaTime; if (isOutZone) { outZoneTimer += Time.deltaTime; if (outZoneTimer < 2f || (outZoneTimer > 4f && outZoneTimer < 6f) || outZoneTimer > 8f) { outOfZone.enabled = true; } else { outOfZone.enabled = false; } if (outZoneTimer > 10f) { GameObject worldCenter = GameObject.Find("worldCenter"); Vector3 worldToPlayer = player.transform.position - worldCenter.transform.position; player.transform.position = worldCenter.transform.position + worldToPlayer.normalized * worldCenter.GetComponent <outZone>().GetRadius(); player.transform.forward = -worldToPlayer.normalized; outOfZone.enabled = false; } } }