Created
February 21, 2018 03:00
-
-
Save ray5527880/4de267fc318e72f83e0b76d20263ee5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class MyCamera : MonoBehaviour { | |
[Header("Camera Properties")] | |
public float DistanceAway; //how far the camera is from the player. | |
public float DistanceUp; //how high the camera is above the player | |
public float smooth = 4.0f; //how smooth the camera moves into place | |
public float rotateAround = 70f; //the angle at which you will rotate the camera (on an axis) | |
[Header("Player to follow")] | |
public Transform target; //the target the camera follows | |
[Header("Layer(s) to include")] | |
public LayerMask CamOcclusion; //the layers that will be affected by collision | |
[Header("OnCollision")] | |
// public worldVectorMap wvm; | |
RaycastHit hit; | |
float cameraHeight = 55f; | |
float cameraPan = 0f; | |
float camRotateSpeed = 180f; | |
//--------------------------- | |
float R_ver;//儲存滑鼠水平移動值 | |
float R_hor;//儲存滑鼠垂直移動值 | |
//--------------------------- | |
Vector3 camPosition; | |
Vector3 camMask; | |
Vector3 followMask; | |
// Use this for initialization | |
void Start() | |
{ | |
//the statement below automatically positions the camera behind the target. | |
rotateAround = target.eulerAngles.y - 45f; | |
} | |
void Update() | |
{ | |
if (Input.GetAxis("Mouse X") != 0)//讀取滑鼠水平移動值 | |
{ | |
R_ver = Input.GetAxis("Mouse X"); | |
} | |
else | |
{ | |
R_ver = 0; | |
} | |
if (Input.GetAxis("Mouse Y") != 0)//讀取滑鼠垂直移動值 | |
{ | |
R_hor = Input.GetAxis("Mouse Y") / 5; | |
} | |
else | |
{ | |
R_hor = 0; | |
} | |
} | |
// Update is called once per frame | |
//621168 | |
void LateUpdate() | |
{ | |
//Offset of the targets transform (Since the pivot point is usually at the feet). | |
//-------------------------------------------------- | |
rotateAround += R_ver * 3; | |
if (R_hor != 0) | |
{ | |
DistanceUp += R_hor; | |
if (DistanceUp > 3)//限制高度 | |
{ | |
DistanceUp = 3; | |
} | |
if (DistanceUp < -3.5f)//限制高度 | |
{ | |
DistanceUp = -3.5f; | |
} | |
} | |
if ((transform.position - target.position).magnitude < 3.5f)//攝影機距離 | |
{ | |
if (DistanceUp >= 0) | |
{ | |
DistanceAway = 2.3f - (DistanceUp * DistanceUp * 0.07f) - (DistanceUp * 0.09f); | |
} | |
} | |
//--------------------------------------- | |
// DistanceAway -=Mathf.Abs(keyboardctrl.R_hor()/10); | |
// DistanceUp += keyboardctrl.R_hor()/10; | |
Vector3 targetOffset = new Vector3(target.position.x, target.position.y, target.position.z); | |
Quaternion rotation = Quaternion.Euler(cameraHeight, rotateAround, cameraPan); | |
Vector3 vectorMask = Vector3.one; | |
Vector3 rotateVector = rotation * vectorMask; | |
//this determines where both the camera and it's mask will be. | |
//the camMask is for forcing the camera to push away from walls. | |
camPosition = targetOffset + Vector3.up * DistanceUp - rotateVector * DistanceAway; | |
camMask = targetOffset + Vector3.up * DistanceUp - rotateVector * DistanceAway; | |
//Debug.Log("camMask:"+camMask+","+(transform.position-target.transform.position).magnitude); | |
occludeRay(ref targetOffset); | |
smoothCamMethod(); | |
transform.LookAt(target); | |
#region wrap the cam orbit rotation | |
if (rotateAround > 360) | |
{ | |
rotateAround = 0f; | |
} | |
else if (rotateAround < 0f) | |
{ | |
rotateAround = (rotateAround + 360f); | |
} | |
#endregion | |
//rotateAround += customPreferences.HorizontalAxis2 * camRotateSpeed * Time.deltaTime; | |
//DistanceUp = Mathf.Clamp(DistanceUp += customPreferences.VerticalAxis2, -0.79f, 2.3f); | |
} | |
void smoothCamMethod() | |
{ | |
smooth = 10f; | |
transform.position = Vector3.Lerp(transform.position, camPosition, Time.deltaTime * smooth); | |
} | |
void occludeRay(ref Vector3 targetFollow) | |
{ | |
#region prevent wall clipping | |
//declare a new raycast hit. | |
RaycastHit wallHit = new RaycastHit(); | |
//linecast from your player (targetFollow) to your cameras mask (camMask) to find collisions., CamOcclusion | |
if (Physics.Linecast(targetFollow, camMask, out wallHit, CamOcclusion)) | |
{ | |
//the smooth is increased so you detect geometry collisions faster. | |
smooth = 10f; | |
//the x and z coordinates are pushed away from the wall by hit.normal. | |
//the y coordinate stays the same. | |
camPosition = new Vector3(wallHit.point.x + wallHit.normal.x * 0.5f, camPosition.y + wallHit.normal.y * 0.5f, wallHit.point.z + wallHit.normal.z * 0.5f); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Modify others, sources