using UnityEngine;
using System.Collections;
public class Cam : MonoBehaviour
{
public bool CanMove = false;
private Vector3 oldMousePos;
private Vector3 newMosuePos;
private Texture2D gogj;
public GameObject currentCamera = null;
public GameObject currentCam_root = null;
public float moveSpeed = 0.1f;
public float rotSpeed = 15.0f;
public float scalSensetive = 10.0f;
public float minFov = 15.0f;
public float maxFov = 90.0f;
public float minimumY = -60F;
public float maximumY = 60F;
public float personHeight = 2.0F;
public float middleMoveSpeed = 0.1F;//鼠标中间控制相机的速度
public GameObject cube;
private Vector3 origiPos;
private Vector3 origiUp;
private Vector3 origiRight;
private string viewString = "Person Veiw";
public enum ViewMode { SuperView = 0, PersonView = 1 }
public ViewMode camView = ViewMode.PersonView;
void Start()
{
if (currentCamera == null)
currentCamera = GameObject.Find("MainCamera");
origiPos = currentCamera.transform.position;
origiUp = currentCamera.transform.up;
origiRight = currentCamera.transform.right;
//currentCamera.rigidbody.freezeRotation = true;
oldMousePos = newMosuePos = Input.mousePosition;
}
void Cam_ShengJiang(int i)
{
transform.Translate(Vector3.up * i * Time.deltaTime);
}
// Update is called once per frame
void FixedUpdate()
{
if (!CanMove)
{
return;
}
if (camView == ViewMode.PersonView)
{
moveCamera();
scaleCamera();
rotateCamera();
terrainDetect();
}
if (camView == ViewMode.SuperView)
{
moveCamera();
scaleCamera();
superViewMouse();
}
oldMousePos = Input.mousePosition;
}
void moveCamera()
{
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(new Vector3(-moveSpeed, 0, 0), Space.Self);
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(new Vector3(moveSpeed, 0, 0), Space.Self);
}
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(new Vector3(0, 0, -moveSpeed), Space.World);
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(new Vector3(0, 0, moveSpeed), Space.World);
}
if (Input.GetKey(KeyCode.Q) && (camView == ViewMode.SuperView))
{
transform.Translate(new Vector3(0, moveSpeed, 0), Space.World);
}
if (Input.GetKey(KeyCode.E) && (camView == ViewMode.SuperView))
{
transform.Translate(new Vector3(0, -moveSpeed, 0), Space.World);
}
}
void rotateCamera()
{
if (Input.GetMouseButton(0))
{
newMosuePos = Input.mousePosition;
Vector3 dis = newMosuePos - oldMousePos;
float angleX = dis.x * rotSpeed * Time.deltaTime * 0.5f;
float angleY = dis.y * rotSpeed * Time.deltaTime * 0.5f;
transform.Rotate(new Vector3(-angleY, 0, 0), Space.Self);
transform.Rotate(new Vector3(0, angleX, 0), Space.World);
}
//oldMousePos=newMosuePos=Input.mousePosition;
}
void scaleCamera()
{
//float fov = currentCamera.camera.fieldOfView;
//fov -= Input.GetAxis("Mouse ScrollWheel") * scalSensetive;
//fov = Mathf.Clamp(fov, minFov, maxFov);
//currentCamera.camera.fieldOfView = fov;
}
void terrainDetect()
{
Ray ray = new Ray(currentCamera.transform.position, Vector3.down);
RaycastHit rayHit;
if (Physics.Raycast(ray, out rayHit, 10))
{
Vector3 oldVec = currentCamera.transform.position;
currentCamera.transform.position = new Vector3(oldVec.x, rayHit.point.y + personHeight, oldVec.z);
}
}
void middleMove()
{
//鼠标中键控制相机的移动
if (Input.GetMouseButton(1))
{
newMosuePos = Input.mousePosition;
Vector3 dis2 = newMosuePos - oldMousePos;
Debug.Log("Dis2 X " + dis2.x + " Y" + dis2.y);
currentCamera.transform.Translate(new Vector3(-dis2.x * middleMoveSpeed * Time.deltaTime, 0, 0), Space.Self);
currentCamera.transform.Translate(new Vector3(0, -dis2.y * middleMoveSpeed * Time.deltaTime, 0), Space.Self);
}
}
void superViewMouse()
{
if (Input.GetAxis("Fire3") == 1)
{
newMosuePos = Input.mousePosition;
Vector3 dis2 = newMosuePos - oldMousePos;
Debug.Log("Dis2 X " + dis2.x + " Y" + dis2.y);
currentCamera.transform.Translate(new Vector3(-dis2.x * middleMoveSpeed * Time.deltaTime, 0, 0), Space.Self);
currentCamera.transform.Translate(new Vector3(0, -dis2.y * middleMoveSpeed * Time.deltaTime, 0), Space.Self);
}
if (Input.GetMouseButton(0))
{
newMosuePos = Input.mousePosition;
Vector3 dis = newMosuePos - oldMousePos;
float angleX = dis.x * rotSpeed * Time.deltaTime;
float angleY = dis.y * rotSpeed * Time.deltaTime;
currentCamera.transform.Rotate(new Vector3(-angleY, 0, 0), Space.Self);
transform.Rotate(new Vector3(0, angleX, 0), Space.World);
}
}
}
上面这个直接绑定到摄像机上即可。
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public enum CamType
{
标贯,
圆锥
}
public class Mouselook : MonoBehaviour {
private CharacterController controller;
private Vector3 moveDirection = Vector3.zero;
public Transform currentCamera = null;
public float moveSpeed = 1f;//移动速度
public float moveUpAndDownSpeed = 1f;//摄像机上下速度
public float rotSpeed = 15.0f;//旋转速度
public float minXAngle = 88F;//最小角度
public float maxXAngle = 330F;//最大角度
public float minCamHeight = 0f;//摄像机最小高度
public float maxCamHeight = 1f;//摄像机最大高度
public bool bMoveByQE = false;//QE上下移动
public static bool canMove = true;
public CamType ct = CamType.标贯;
void Start()
{
if (currentCamera == null)
currentCamera = this.GetComponentInChildren<Camera>().transform;
controller = gameObject.GetComponent<CharacterController>() ?? gameObject.AddComponent<CharacterController>();
controller.slopeLimit = 65;
controller.height = 1;
controller.radius = 0.3f;
//transform.position = new Vector3(0, 0,0);
//currentCamera.transform.localPosition = new Vector3(0, 2.5f, 0);
moveSpeed = 8;
moveUpAndDownSpeed = 0.5f;
rotSpeed = 8.0f;
maxCamHeight = 3f;
}
// Update is called once per frame
void FixedUpdate()
{
currentCamera.localEulerAngles = new Vector3(currentCamera.localEulerAngles.x, currentCamera.localEulerAngles.y, 0);
if (!canMove) return;
Cam_Move();
rotateCamera();
MoveUpAndDown();
oldMousePos = Input.mousePosition;
}
private void Cam_Move()
{
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= moveSpeed;
}
moveDirection.y -= 20 * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
void MoveUpAndDown()
{
float f = currentCamera.transform.localPosition.y;
if (bMoveByQE)
{
if (Input.GetKey(KeyCode.Q))
{
f = currentCamera.transform.localPosition.y + moveUpAndDownSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.E))
{
f = currentCamera.transform.localPosition.y - moveUpAndDownSpeed * Time.deltaTime;
}
}
else
{
f = Input.GetAxis("Mouse ScrollWheel") * moveUpAndDownSpeed + currentCamera.transform.localPosition.y;
}
f = Mathf.Clamp(f, minCamHeight, maxCamHeight);
currentCamera.transform.localPosition = new Vector3(0, f, 0);
}
private Vector3 oldMousePos;
private Vector3 newMosuePos;
void rotateCamera()
{
if (EventSystem.current.IsPointerOverGameObject()) return;
if (ct == CamType.标贯)
{
if (!WS_BZManager.instance.CamCanMove)
{
return;
}
}
if (Input.GetMouseButton(0))//&&!EventSystem.current.IsPointerOverGameObject())
{
//float mousX = Input.GetAxis("Mouse X");
//this.transform.transform.Rotate(0, mousX * rotSpeed, 0);
//float mousY = Input.GetAxis("Mouse Y");
//currentCamera.transform.Rotate(-mousY * Time.timeScale, 0, 0);
//if (currentCamera.transform.eulerAngles.x > minXAngle && currentCamera.transform.eulerAngles.x < 180)
//{
// currentCamera.transform.eulerAngles = new Vector3(minXAngle, currentCamera.transform.eulerAngles.y,
// currentCamera.transform.eulerAngles.z);
//}
//if (currentCamera.transform.eulerAngles.x < maxXAngle && currentCamera.transform.eulerAngles.x > 180)
//{
// currentCamera.transform.eulerAngles = new Vector3(maxXAngle, currentCamera.transform.eulerAngles.y,
// currentCamera.transform.eulerAngles.z);
//}
newMosuePos = Input.mousePosition;
Vector3 dis = newMosuePos - oldMousePos;
float angleX = dis.x * rotSpeed * Time.deltaTime * 0.5f;
float angleY = dis.y * rotSpeed * Time.deltaTime * 0.5f;
currentCamera.transform.Rotate(new Vector3(-angleY, 0, 0), Space.Self);
this.transform.Rotate(new Vector3(0, angleX, 0), Space.World);
}
}
Transform targetTra;
Camera camUI;
public void RayUI()
{
Vector3 mp = Input.mousePosition;
RaycastHit hitInfo;
Ray ray = camUI.ScreenPointToRay(new Vector3(mp.x, mp.y, 0f));
if (Physics.Raycast(ray.origin, ray.direction, out hitInfo))
{
print(hitInfo.collider.gameObject.transform.root.name.ToString());
if (hitInfo.collider.gameObject.transform.root.name == "UI Root")
targetTra = hitInfo.collider.transform;
}
else
targetTra = null;
}
}
这一个是需要一个charactercontroller控制器,控制器下层创建一个摄像机。然后绑上这个脚本就行了