滑动屏幕旋转摄像机
using UnityEngine;
public class CameraSwitch : MonoBehaviour
{
private float lastPosX;
private float lastPosY;
public Transform CameraX;
public Transform CameraY;
private float RotaSpeed ;
public float Speed = 100;
private float range = 2;
void Update()
{
RotaSpeed = Speed * Time.deltaTime;
if (Input.GetMouseButton(1))
{
if (Input.mousePosition.x > lastPosX + range)
{
CameraX.Rotate(Vector3.up, RotaSpeed);
lastPosX = Input.mousePosition.x;
}
else if (Input.mousePosition.x < lastPosX - range)
{
CameraX.Rotate(Vector3.up, -RotaSpeed);
lastPosX = Input.mousePosition.x;
}
if (Input.mousePosition.y > lastPosY + range)
{
CameraY.Rotate(Vector3.right, -RotaSpeed);
lastPosY = Input.mousePosition.y;
}
else if (Input.mousePosition.y < lastPosY - range)
{
CameraY.Rotate(Vector3.right, RotaSpeed);
lastPosY = Input.mousePosition.y;
}
}
}
}
第三视角相机跟随
using UnityEngine;
public class CameraFollower : MonoBehaviour
{
public Transform Target;
public Transform Offset;
private float mMoveSpeed=1f;
private Vector3 offset;
private void Start()
{
offset = Target.position - Offset.position;
}
void Update()
{
transform.position = Vector3.Lerp(transform.position, Target.position-offset, Time.deltaTime * mMoveSpeed);
}
}
视角互相切换
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ViewCtrl : MonoBehaviour
{
public Button ViewChangeBtn;
private bool firstViewState=true;
public Transform FirstCamera;
public Transform ThirdCamera;
public Transform Offset;
private void Start()
{
ViewChangeBtn.onClick.AddListener(ChangeView);
FirstCamera.gameObject.SetActive(firstViewState);
ThirdCamera.gameObject.SetActive(!firstViewState);
}
private void ChangeView()
{
firstViewState = !firstViewState;
FirstCamera.gameObject.SetActive(firstViewState);
if(!firstViewState)
{
ThirdCamera.position = Offset.position;
}
ThirdCamera.gameObject.SetActive(!firstViewState);
}
}