From da8b5b2eceb2a300cda68de88efedd3c5446bc02 Mon Sep 17 00:00:00 2001 From: Sagittaeri Date: Sun, 22 Jan 2017 10:02:58 +1100 Subject: [PATCH] custom camera follow --- .../Assets/Scripts/CustomSmoothFollow.cs | 70 ++++++++++++++++++ .../Assets/Scripts/CustomSmoothFollow.cs.meta | 12 +++ .../Assets/_Scenes/LighthouseTestScene.unity | Bin 114152 -> 115656 bytes .../ProjectSettings/TagManager.asset | Bin 4316 -> 4332 bytes 4 files changed, 82 insertions(+) create mode 100644 IronToad_UnityProject/Assets/Scripts/CustomSmoothFollow.cs create mode 100644 IronToad_UnityProject/Assets/Scripts/CustomSmoothFollow.cs.meta diff --git a/IronToad_UnityProject/Assets/Scripts/CustomSmoothFollow.cs b/IronToad_UnityProject/Assets/Scripts/CustomSmoothFollow.cs new file mode 100644 index 0000000..5f29e43 --- /dev/null +++ b/IronToad_UnityProject/Assets/Scripts/CustomSmoothFollow.cs @@ -0,0 +1,70 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CustomSmoothFollow : MonoBehaviour +{ + public Transform target; + public float distance = -200.0f; + public float height = 200.0f; + public float altDistance = -100.0f; + public float altHeight = 250.0f; + public Vector3 damping; + public string layerToMask = "CameraObstruct"; + private int layerMask; + + // Use this for initialization + void Start() + { + layerMask = LayerMask.NameToLayer(layerToMask); + } + + // Update is called once per frame + void LateUpdate() + { + // Early out if we don't have a target + if (!target) + return; + + // Calculate the current and wanted height / XZ pos + float wantedHeight = target.position.y + height; + float wantedDistance = target.position.z + distance; + float wantedSide = target.position.x; + + bool isBlocked = false; + Vector3 wantedPos = new Vector3(wantedSide, wantedHeight, wantedDistance); + RaycastHit hitInfo; + if (Physics.Raycast(wantedPos, target.position-wantedPos, out hitInfo, (target.position-wantedPos).magnitude * 2f)) + { + if (hitInfo.collider.gameObject.layer == layerMask) + isBlocked = true; + } + + if (isBlocked) + { + wantedHeight = target.position.y + altHeight; + wantedDistance = target.position.z + altDistance; + } + + float currentHeight = transform.position.y; + float currentDistance = transform.position.z; + float currentSide = transform.position.x; + + // Damp the height + currentHeight = Mathf.Lerp(currentHeight, wantedHeight, damping.y * Time.deltaTime); + currentDistance = Mathf.Lerp(currentDistance, wantedDistance, damping.z * Time.deltaTime); + currentSide = Mathf.Lerp(currentSide, wantedSide, damping.x * Time.deltaTime); + + // Set the position of the camera on the x-z plane to: + // distance meters behind the target + transform.position = target.position; + + // Set the height of the camera + transform.position = new Vector3(currentSide, currentHeight, currentDistance); + + // Always look at the target + Vector3 lookTarget = target.position; + lookTarget.x = transform.position.x; + transform.LookAt(lookTarget); + } +} diff --git a/IronToad_UnityProject/Assets/Scripts/CustomSmoothFollow.cs.meta b/IronToad_UnityProject/Assets/Scripts/CustomSmoothFollow.cs.meta new file mode 100644 index 0000000..4143e9b --- /dev/null +++ b/IronToad_UnityProject/Assets/Scripts/CustomSmoothFollow.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 3d53f052d067848d086ad46c7435ec07 +timeCreated: 1485036748 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/IronToad_UnityProject/Assets/_Scenes/LighthouseTestScene.unity b/IronToad_UnityProject/Assets/_Scenes/LighthouseTestScene.unity index 47c23a7dce3e14a79500f54f69303e464f014741..8303cde46115eb4b302e2031d40913a24f77dc50 100644 GIT binary patch delta 5499 zcmY+I3shBA8ppqn3wTvP5Rj|+xTq+a0;U$eQEVjBq@|Pi7`-S6K2StVH0$2V2fm6z z+}K(cn$=Z%Tw^tt$_i^TdO6p0W|+nqDQzugY^^Dqie57Reb0B=c67VX_uKn?`+Mzu zj<@}%=$4&b1`-e3Mf^?+5hdWh0X>b)7@Lu`Vl-Xv?Cj)QP7Z9YN{^*)CR}=^XvmsN zU#4EmT+ujn%K6S=$0}D%oU^rK_}|Cezb$hp9`sRT!ksfCcwBAj=ebdb0di{BuTH65 z`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KmeN2!3IQ2sG0 zmgNWVwEY-tOpxCy;66q++4>ubmWx`cOj=)tttoAEgsk#l4HC(lN&WX8rvx@l+P&0U zzJ8oaDc*PcIQf)Ch{+`RpEv2p(4xOa#KYx(8!Fo}X_$2Ru))$!>t#n9{(oslJAEBS YiN31SDvo`9x6e{Bdu620b)KB`fBBk&yZ`_I diff --git a/IronToad_UnityProject/ProjectSettings/TagManager.asset b/IronToad_UnityProject/ProjectSettings/TagManager.asset index 91c855e39493eb2ca0dbe21843d5b8d36730d51b..de2d8372e6fe6f6ec211e5246b6c952a60267170 100644 GIT binary patch delta 61 zcmV-D0K)&=A?zUt0008{000o|u?X}60qnB@1OE(@5Ca5~5M&Y#0000(VQpn{VNYUn TbaHiLbdv!CUXl<5lgJPhQ?U~9 delta 42 zcmV+_0M-BOA>1Jd0008{000o&u?X}60o=0z1OE(@5Ca5~5M+}J17MO61e4eh6em*+ AE&u=k