ATC522-011\IGDA 7 years ago
parent
commit
30f28f3c03
3 changed files with 82 additions and 0 deletions
  1. +70
    -0
      IronToad_UnityProject/Assets/Scripts/CustomSmoothFollow.cs
  2. +12
    -0
      IronToad_UnityProject/Assets/Scripts/CustomSmoothFollow.cs.meta
  3. BIN
      IronToad_UnityProject/Assets/_Scenes/LighthouseTestScene.unity

+ 70
- 0
IronToad_UnityProject/Assets/Scripts/CustomSmoothFollow.cs View File

@ -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);
}
}

+ 12
- 0
IronToad_UnityProject/Assets/Scripts/CustomSmoothFollow.cs.meta View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 3d53f052d067848d086ad46c7435ec07
timeCreated: 1485036748
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

BIN
IronToad_UnityProject/Assets/_Scenes/LighthouseTestScene.unity View File


Loading…
Cancel
Save