mirror of
https://github.com/ConjureETS/OuijaMTLGJ2016.git
synced 2026-03-26 03:01:06 +00:00
add anchored and fixed camera behavior tests
This commit is contained in:
parent
f731d34fda
commit
ea674245cf
1814
Assets/scenes/anchoredCameraTest.unity
Normal file
1814
Assets/scenes/anchoredCameraTest.unity
Normal file
File diff suppressed because it is too large
Load Diff
8
Assets/scenes/anchoredCameraTest.unity.meta
Normal file
8
Assets/scenes/anchoredCameraTest.unity.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 0dd8820e1cb718b47a2700b22d68f73e
|
||||||
|
timeCreated: 1454194226
|
||||||
|
licenseType: Free
|
||||||
|
DefaultImporter:
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
1814
Assets/scenes/fixedCameraTest.unity
Normal file
1814
Assets/scenes/fixedCameraTest.unity
Normal file
File diff suppressed because it is too large
Load Diff
8
Assets/scenes/fixedCameraTest.unity.meta
Normal file
8
Assets/scenes/fixedCameraTest.unity.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 0f22821b69d2f744cb9e9f9c6a077f9a
|
||||||
|
timeCreated: 1454198156
|
||||||
|
licenseType: Free
|
||||||
|
DefaultImporter:
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
102
Assets/scripts/MainCameraBehavior.cs
Normal file
102
Assets/scripts/MainCameraBehavior.cs
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
|
public class MainCameraBehavior : MonoBehaviour {
|
||||||
|
|
||||||
|
public Transform targetTransform;
|
||||||
|
public float y = 2.5f; // store the height value since it will never change
|
||||||
|
public float offsetZ = -2.2f;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public float smooth = 1.5f; // The relative speed at which the camera will catch up.
|
||||||
|
|
||||||
|
|
||||||
|
private Vector3 relCameraPos; // The relative position of the camera from the targetTransform.
|
||||||
|
private float relCameraPosMag; // The distance of the camera from the targetTransform.
|
||||||
|
private Vector3 newPos; // The position the camera is trying to reach.
|
||||||
|
|
||||||
|
|
||||||
|
void Awake()
|
||||||
|
{
|
||||||
|
// Setting the relative position as the initial relative position of the camera in the scene.
|
||||||
|
relCameraPos = transform.position - targetTransform.position;
|
||||||
|
relCameraPosMag = relCameraPos.magnitude - 0.5f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void FixedUpdate()
|
||||||
|
{
|
||||||
|
// The standard position of the camera is the relative position of the camera from the targetTransform.
|
||||||
|
Vector3 standardPos = targetTransform.position + relCameraPos;
|
||||||
|
|
||||||
|
// The abovePos is directly above the targetTransform at the same distance as the standard position.
|
||||||
|
Vector3 abovePos = targetTransform.position + Vector3.up * relCameraPosMag;
|
||||||
|
|
||||||
|
// An array of 5 points to check if the camera can see the targetTransform.
|
||||||
|
Vector3[] checkPoints = new Vector3[5];
|
||||||
|
|
||||||
|
// The first is the standard position of the camera.
|
||||||
|
checkPoints[0] = standardPos;
|
||||||
|
|
||||||
|
// The next three are 25%, 50% and 75% of the distance between the standard position and abovePos.
|
||||||
|
checkPoints[1] = Vector3.Lerp(standardPos, abovePos, 0.25f);
|
||||||
|
checkPoints[2] = Vector3.Lerp(standardPos, abovePos, 0.5f);
|
||||||
|
checkPoints[3] = Vector3.Lerp(standardPos, abovePos, 0.75f);
|
||||||
|
|
||||||
|
// The last is the abovePos.
|
||||||
|
checkPoints[4] = abovePos;
|
||||||
|
|
||||||
|
// Run through the check points...
|
||||||
|
for (int i = 0; i < checkPoints.Length; i++)
|
||||||
|
{
|
||||||
|
// ... if the camera can see the targetTransform...
|
||||||
|
if (ViewingPosCheck(checkPoints[i]))
|
||||||
|
// ... break from the loop.
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lerp the camera's position between it's current position and it's new position.
|
||||||
|
transform.position = Vector3.Lerp(transform.position, newPos, smooth * Time.deltaTime);
|
||||||
|
|
||||||
|
// Make sure the camera is looking at the targetTransform.
|
||||||
|
SmoothLookAt();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool ViewingPosCheck(Vector3 checkPos)
|
||||||
|
{
|
||||||
|
RaycastHit hit;
|
||||||
|
|
||||||
|
// If a raycast from the check position to the targetTransform hits something...
|
||||||
|
if (Physics.Raycast(checkPos, targetTransform.position - checkPos, out hit, relCameraPosMag))
|
||||||
|
// ... if it is not the targetTransform...
|
||||||
|
if (hit.transform != targetTransform)
|
||||||
|
// This position isn't appropriate.
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// If we haven't hit anything or we've hit the targetTransform, this is an appropriate position.
|
||||||
|
newPos = checkPos;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void SmoothLookAt()
|
||||||
|
{
|
||||||
|
// Create a vector from the camera towards the targetTransform.
|
||||||
|
Vector3 reltargetTransformPosition = targetTransform.position - transform.position;
|
||||||
|
|
||||||
|
// Create a rotation based on the relative position of the targetTransform being the forward vector.
|
||||||
|
Quaternion lookAtRotation = Quaternion.LookRotation(reltargetTransformPosition, Vector3.up);
|
||||||
|
|
||||||
|
// Lerp the camera's rotation between it's current rotation and the rotation that looks at the targetTransform.
|
||||||
|
transform.rotation = Quaternion.Lerp(transform.rotation, lookAtRotation, smooth * Time.deltaTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
12
Assets/scripts/MainCameraBehavior.cs.meta
Normal file
12
Assets/scripts/MainCameraBehavior.cs.meta
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 4ff49a309510a6849bd5534bae4657b1
|
||||||
|
timeCreated: 1454194553
|
||||||
|
licenseType: Free
|
||||||
|
MonoImporter:
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
102
Assets/scripts/MainCameraBehaviorFixed.cs
Normal file
102
Assets/scripts/MainCameraBehaviorFixed.cs
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
|
public class MainCameraBehaviorFixed : MonoBehaviour {
|
||||||
|
|
||||||
|
public Transform targetTransform;
|
||||||
|
public float y = 2.5f; // store the height value since it will never change
|
||||||
|
public float offsetZ = -2.2f;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public float smooth = 1.5f; // The relative speed at which the camera will catch up.
|
||||||
|
|
||||||
|
|
||||||
|
private Vector3 relCameraPos; // The relative position of the camera from the targetTransform.
|
||||||
|
private float relCameraPosMag; // The distance of the camera from the targetTransform.
|
||||||
|
private Vector3 newPos; // The position the camera is trying to reach.
|
||||||
|
|
||||||
|
|
||||||
|
void Awake()
|
||||||
|
{
|
||||||
|
// Setting the relative position as the initial relative position of the camera in the scene.
|
||||||
|
relCameraPos = transform.position - targetTransform.position;
|
||||||
|
relCameraPosMag = relCameraPos.magnitude - 0.5f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void FixedUpdate()
|
||||||
|
{
|
||||||
|
// The standard position of the camera is the relative position of the camera from the targetTransform.
|
||||||
|
Vector3 standardPos = targetTransform.position + relCameraPos;
|
||||||
|
|
||||||
|
// The abovePos is directly above the targetTransform at the same distance as the standard position.
|
||||||
|
Vector3 abovePos = targetTransform.position + Vector3.up * relCameraPosMag;
|
||||||
|
|
||||||
|
// An array of 5 points to check if the camera can see the targetTransform.
|
||||||
|
Vector3[] checkPoints = new Vector3[5];
|
||||||
|
|
||||||
|
// The first is the standard position of the camera.
|
||||||
|
checkPoints[0] = standardPos;
|
||||||
|
|
||||||
|
// The next three are 25%, 50% and 75% of the distance between the standard position and abovePos.
|
||||||
|
checkPoints[1] = Vector3.Lerp(standardPos, abovePos, 0.25f);
|
||||||
|
checkPoints[2] = Vector3.Lerp(standardPos, abovePos, 0.5f);
|
||||||
|
checkPoints[3] = Vector3.Lerp(standardPos, abovePos, 0.75f);
|
||||||
|
|
||||||
|
// The last is the abovePos.
|
||||||
|
checkPoints[4] = abovePos;
|
||||||
|
|
||||||
|
// Run through the check points...
|
||||||
|
for (int i = 0; i < checkPoints.Length; i++)
|
||||||
|
{
|
||||||
|
// ... if the camera can see the targetTransform...
|
||||||
|
if (ViewingPosCheck(checkPoints[i]))
|
||||||
|
// ... break from the loop.
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lerp the camera's position between it's current position and it's new position.
|
||||||
|
//transform.position = Vector3.Lerp(transform.position, newPos, smooth * Time.deltaTime);
|
||||||
|
|
||||||
|
// Make sure the camera is looking at the targetTransform.
|
||||||
|
SmoothLookAt();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool ViewingPosCheck(Vector3 checkPos)
|
||||||
|
{
|
||||||
|
RaycastHit hit;
|
||||||
|
|
||||||
|
// If a raycast from the check position to the targetTransform hits something...
|
||||||
|
if (Physics.Raycast(checkPos, targetTransform.position - checkPos, out hit, relCameraPosMag))
|
||||||
|
// ... if it is not the targetTransform...
|
||||||
|
if (hit.transform != targetTransform)
|
||||||
|
// This position isn't appropriate.
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// If we haven't hit anything or we've hit the targetTransform, this is an appropriate position.
|
||||||
|
newPos = checkPos;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void SmoothLookAt()
|
||||||
|
{
|
||||||
|
// Create a vector from the camera towards the targetTransform.
|
||||||
|
Vector3 reltargetTransformPosition = targetTransform.position - transform.position;
|
||||||
|
|
||||||
|
// Create a rotation based on the relative position of the targetTransform being the forward vector.
|
||||||
|
Quaternion lookAtRotation = Quaternion.LookRotation(reltargetTransformPosition, Vector3.up);
|
||||||
|
|
||||||
|
// Lerp the camera's rotation between it's current rotation and the rotation that looks at the targetTransform.
|
||||||
|
transform.rotation = Quaternion.Lerp(transform.rotation, lookAtRotation, smooth * Time.deltaTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
12
Assets/scripts/MainCameraBehaviorFixed.cs.meta
Normal file
12
Assets/scripts/MainCameraBehaviorFixed.cs.meta
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 5d44c30752f2162458b447fc7aa8fa3d
|
||||||
|
timeCreated: 1454198262
|
||||||
|
licenseType: Free
|
||||||
|
MonoImporter:
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Loading…
x
Reference in New Issue
Block a user