void Update() { if (Input.GetMouseButton(1)) // right mouse click starts lock on mode { if (followTarget == null) { // find a viable target Targetable[] targets = FindObjectsOfType <Targetable>(); if (targets != null && targets.Length > 0) { // add target acquiring by distance and angle to find the best target // also add target switching via input followTarget = targets[0]; } } if (followTarget != null) { ff.cameraEnabled = false; // disable freeform control Vector3 dirToTarget = (followTarget.transform.position + followTarget.offset) - transform.position; Quaternion toRotation = Quaternion.LookRotation(dirToTarget, Vector3.up); cc.transform.rotation = Quaternion.Slerp(cc.transform.rotation, toRotation, Time.deltaTime * rotationSpeed); } } else if (followTarget != null) { followTarget = null; ff.cameraEnabled = true; // enable freeform control again } }
void Update() { if (Input.GetMouseButton(1)) // right mouse click starts lock on mode { if (followTarget == null) { // find a viable target Targetable[] targets = FindObjectsOfType<Targetable>(); if (targets != null && targets.Length > 0) { // add target acquiring by distance and angle to find the best target // also add target switching via input followTarget = targets[0]; } } if (followTarget != null) { ff.cameraEnabled = false; // disable freeform control Vector3 dirToTarget = (followTarget.transform.position + followTarget.offset) - transform.position; Quaternion toRotation = Quaternion.LookRotation(dirToTarget, Vector3.up); cc.transform.rotation = Quaternion.Slerp(cc.transform.rotation, toRotation, Time.deltaTime * rotationSpeed); } } else if (followTarget != null) { followTarget = null; ff.cameraEnabled = true; // enable freeform control again } }
void Update() { if (Input.GetMouseButton(1)) // right mouse click starts lock on mode { if (followTarget == null) { // find a viable target targets = new List <Targetable>(FindObjectsOfType <Targetable>()); if (targets != null && targets.Count > 0) { // add target acquiring by distance // TODO get angle to find the best target // TODO add target switching via input List <TargetableWithDistance> items = new List <TargetableWithDistance>(targets.Count); for (int i = 0; i < targets.Count; i++) { items.Add(new TargetableWithDistance() { target = targets[i], distance = (targets[i].transform.position - cc.target.transform.position).magnitude }); } items.Sort(sortTargetsMethod); followTarget = items[0].target; } } if (followTarget != null) { ff.cameraEnabled = false; // disable freeform control Vector3 dirToTarget = (followTarget.transform.position + followTarget.offset) - transform.position; Quaternion toRotation = Quaternion.LookRotation(dirToTarget, Vector3.up); cc.transform.rotation = Quaternion.Slerp(cc.transform.rotation, toRotation, Time.deltaTime * rotationSpeed); } } else if (followTarget != null) { followTarget = null; ff.cameraEnabled = true; // enable freeform control again } }