public static ObjectToHit FindClosestTarget(Transform viewer, float radius, float fieldOfViewRange, ObjectToHit viewerObject) { List <ObjectToHit> allTargets = TargetsManager.FindTargetsInRadius(viewer.position, radius); Dictionary <ObjectToHit, Vector2> possibleTargets = new Dictionary <ObjectToHit, Vector2>(); //object, Vector2(distance, angle) foreach (ObjectToHit target in allTargets) { // RaycastHit hit; Vector3 rayDirection = target.transform.position - viewer.position; //??? float angle = Vector3.Angle(rayDirection, viewer.forward); if (angle < fieldOfViewRange) { RaycastHit[] hits = Physics.RaycastAll(viewer.position, rayDirection); // if (Physics.RaycastAll(viewer.position, rayDirection, out hit)) foreach (RaycastHit hit in hits) { ObjectToHit obj = hit.transform.GetComponent <ObjectToHit>(); if (obj == target && obj != viewerObject && !possibleTargets.ContainsKey(target)) { //This is possible target possibleTargets.Add(target, new Vector2(Vector3.Distance(viewer.position, target.center.position), angle)); } else { //Target behind the obstancle // return false; } } } } return(GetBestMatch(possibleTargets)); }
public static ObjectToHit Raycast(Transform viewer) { RaycastHit hit; if (Physics.Raycast(viewer.position, viewer.forward, out hit, 10, 1 << 8)) { ObjectToHit objectToHit = hit.collider.GetComponent <ObjectToHit>(); if (objectToHit != null) { return(objectToHit); } } return(null); // RaycastHit[] allHits = Physics.BoxCastAll(transform.position, new Vector3(1, 1, 5), transform.forward, transform.rotation, 10, 1 << 8); // List<ObjectToHit> allObjectsToHit = new List<ObjectToHit>(); // // foreach (RaycastHit hit in allHits) // { // ObjectToHit objectToHit = hit.collider.GetComponent<ObjectToHit>(); // if (objectToHit != null && hit.collider.gameObject != this.gameObject) // { // allObjectsToHit.Add(objectToHit); // Debug.Log(objectToHit.name); // } // } }
public static ObjectToHit FindClosestTarget(Transform viewer, float radius, float fieldOfViewRange, ObjectToHit viewerObject) { List<ObjectToHit> allTargets = TargetsManager.FindTargetsInRadius(viewer.position, radius); Dictionary<ObjectToHit, Vector2> possibleTargets = new Dictionary<ObjectToHit, Vector2>(); //object, Vector2(distance, angle) foreach (ObjectToHit target in allTargets) { // RaycastHit hit; Vector3 rayDirection = target.transform.position - viewer.position; //??? float angle = Vector3.Angle(rayDirection, viewer.forward); if (angle < fieldOfViewRange) { RaycastHit[] hits = Physics.RaycastAll(viewer.position, rayDirection); // if (Physics.RaycastAll(viewer.position, rayDirection, out hit)) foreach (RaycastHit hit in hits) { ObjectToHit obj = hit.transform.GetComponent<ObjectToHit>(); if (obj == target && obj != viewerObject && !possibleTargets.ContainsKey(target)) { //This is possible target possibleTargets.Add(target, new Vector2(Vector3.Distance(viewer.position, target.center.position), angle)); } else { //Target behind the obstancle // return false; } } } } return GetBestMatch(possibleTargets); }
public static bool AddTarget(ObjectToHit newTarget) { if (!allTargets.Contains(newTarget)) { allTargets.Add(newTarget); return(true); } else { return(false); } }
public static bool RemoveTarget(ObjectToHit target) { if (allTargets.Contains(target)) { allTargets.Remove(target); return(true); } else { return(false); } }
void Start() { thisObjectToHit = GetComponent <ObjectToHit>(); m_Animator = GetComponent <Animator>(); m_Rigidbody = GetComponent <Rigidbody>(); m_Capsule = GetComponent <CapsuleCollider>(); m_CapsuleHeight = m_Capsule.height; m_CapsuleCenter = m_Capsule.center; m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ; m_OrigGroundCheckDistance = m_GroundCheckDistance; }
public static bool AddTarget(ObjectToHit newTarget) { if (!allTargets.Contains(newTarget)) { allTargets.Add(newTarget); return true; } else { return false; } }
void Update() { if (NeedToFindNewTarget()) { ObjectToHit nearestTarget = TargetsManager.GetNearestTarget(thisObjectToHit, visibleRadius); target = nearestTarget; // characterControl.SetTarget(target.transform); } // if (target == null) // { // ObjectToHit nearestTarget = TargetsManager.GetNearestTarget(thisObjectToHit, visibleRadius); // if (nearestTarget != null) // { // target = nearestTarget; // } // } if (target != null) { agent.SetDestination(target.transform.position); if (agent.remainingDistance < focusDistance) { movementController.FocusOnTarget(target.transform); } else if (movementController.target != null) { movementController.Unfocus(); } // float distance = Vector3.Distance(transform.position, target.transform.position); if (agent.remainingDistance > agent.stoppingDistance) // if (distance > distanceToHit) { Vector3 direction = target.transform.position - transform.position; movementController.Move(direction, false, true); swordController.Run(); swordController.StopHit(); } else if (agent.velocity.magnitude < velocityToHit) { movementController.Stop(); swordController.Stop(); swordController.DoHit(); } } }
public static List<ObjectToHit> FindTargetsInRadius(ObjectToHit head, float radius) { List<ObjectToHit> targetsInRadius = new List<ObjectToHit>(); foreach (ObjectToHit target in allTargets) { if (target != head) { if (Vector3.Distance(head.center.position, target.center.position) <= radius) { targetsInRadius.Add(target); } } } return targetsInRadius; }
public static ObjectToHit GetBestMatch(Dictionary <ObjectToHit, Vector2> possibleTargets) { float minAngle = 100; ObjectToHit bestMatch = null; foreach (ObjectToHit target in possibleTargets.Keys) { if (possibleTargets[target].y < minAngle) { minAngle = possibleTargets[target].y; bestMatch = target; } } return(bestMatch); }
public static List <ObjectToHit> FindTargetsInRadius(ObjectToHit head, float radius) { List <ObjectToHit> targetsInRadius = new List <ObjectToHit>(); foreach (ObjectToHit target in allTargets) { if (target != head) { if (Vector3.Distance(head.center.position, target.center.position) <= radius) { targetsInRadius.Add(target); } } } return(targetsInRadius); }
void Awake() { if (movementController == null) { movementController = GetComponent <ThirdPersonCharacter>(); } if (swordController == null) { swordController = GetComponent <SwordAIControl>(); } if (thisObjectToHit == null) { thisObjectToHit = GetComponent <ObjectToHit>(); } if (agent == null) { agent = GetComponent <NavMeshAgent>(); } }
public static ObjectToHit GetNearestTarget(ObjectToHit head, float radius) { ObjectToHit nearestTarget = null; float minDistance = 1000; foreach (ObjectToHit target in allTargets) { if (target != head) { float distance = Vector3.Distance(head.center.position, target.center.position); if (distance <= radius && distance < minDistance) { nearestTarget = target; } } } return nearestTarget; }
public static ObjectToHit GetNearestTarget(ObjectToHit head, float radius) { ObjectToHit nearestTarget = null; float minDistance = 1000; foreach (ObjectToHit target in allTargets) { if (target != head) { float distance = Vector3.Distance(head.center.position, target.center.position); if (distance <= radius && distance < minDistance) { nearestTarget = target; } } } return(nearestTarget); }
void Awake() { if (movementController == null) { movementController = GetComponent<ThirdPersonCharacter>(); } if (swordController == null) { swordController = GetComponent<SwordAIControl>(); } if (thisObjectToHit == null) { thisObjectToHit = GetComponent<ObjectToHit>(); } if (agent == null) { agent = GetComponent<NavMeshAgent>(); } }
public static bool RemoveTarget(ObjectToHit target) { if (allTargets.Contains(target)) { allTargets.Remove(target); return true; } else { return false; } }
// Fixed update is called in sync with physics private void Update() { // read inputs float horizontal = CrossPlatformInputManager.GetAxis("Horizontal"); float vertical = CrossPlatformInputManager.GetAxis("Vertical"); // calculate move direction to pass to character if (m_Cam != null) { // calculate camera relative direction to move: m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized; m_Move = vertical * m_CamForward + horizontal * m_Cam.right; } else { // we use world-relative directions in the case of no main camera m_Move = vertical * Vector3.forward + horizontal * Vector3.right; } #if !MOBILE_INPUT // walk speed multiplier if (Input.GetKey(KeyCode.LeftShift)) m_Move *= 0.5f; #endif if (Input.GetKeyDown(KeyCode.F) && targetObject == null) { //Focus on target targetObject = Raycaster.FindClosestTarget(mainCamera, 10, fieldOfViewRange, thisObjectToHit); // Debug.Log("Focus on " + targetObject.ToString()); if (targetObject != null && targetObject != thisObjectToHit) { m_Character.FocusOnTarget(targetObject.center); focusEffect.enabled = true; focusEffect.subject = targetObject.center.transform; StopCoroutine("SetFocus"); StartCoroutine("SetFocus", new Vector2(10, 2)); //Debug.Log("target " + targetObject.name); } } else if (Input.GetKeyDown(KeyCode.F) && targetObject != null) { //Unfocus targetObject = null; m_Character.Unfocus(); StopCoroutine("SetFocus"); StartCoroutine("SetFocus", new Vector2(2, 10)); if (lookAtTweener != null) { lookAtTweener.Kill(); } } // pass all parameters to the character control script m_Character.Move(m_Move, m_Jump, targetObject != null); m_Jump = false; }
// Fixed update is called in sync with physics private void Update() { // read inputs float horizontal = CrossPlatformInputManager.GetAxis("Horizontal"); float vertical = CrossPlatformInputManager.GetAxis("Vertical"); // calculate move direction to pass to character if (m_Cam != null) { // calculate camera relative direction to move: m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized; m_Move = vertical * m_CamForward + horizontal * m_Cam.right; } else { // we use world-relative directions in the case of no main camera m_Move = vertical * Vector3.forward + horizontal * Vector3.right; } #if !MOBILE_INPUT // walk speed multiplier if (Input.GetKey(KeyCode.LeftShift)) { m_Move *= 0.5f; } #endif if (Input.GetKeyDown(KeyCode.F) && targetObject == null) { //Focus on target targetObject = Raycaster.FindClosestTarget(mainCamera, 10, fieldOfViewRange, thisObjectToHit); // Debug.Log("Focus on " + targetObject.ToString()); if (targetObject != null && targetObject != thisObjectToHit) { m_Character.FocusOnTarget(targetObject.center); focusEffect.enabled = true; focusEffect.subject = targetObject.center.transform; StopCoroutine("SetFocus"); StartCoroutine("SetFocus", new Vector2(10, 2)); //Debug.Log("target " + targetObject.name); } } else if (Input.GetKeyDown(KeyCode.F) && targetObject != null) { //Unfocus targetObject = null; m_Character.Unfocus(); StopCoroutine("SetFocus"); StartCoroutine("SetFocus", new Vector2(2, 10)); if (lookAtTweener != null) { lookAtTweener.Kill(); } } // pass all parameters to the character control script m_Character.Move(m_Move, m_Jump, targetObject != null); m_Jump = false; }
void Start() { thisObjectToHit = GetComponent<ObjectToHit>(); m_Animator = GetComponent<Animator>(); m_Rigidbody = GetComponent<Rigidbody>(); m_Capsule = GetComponent<CapsuleCollider>(); m_CapsuleHeight = m_Capsule.height; m_CapsuleCenter = m_Capsule.center; m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ; m_OrigGroundCheckDistance = m_GroundCheckDistance; }