Скрипт:
Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;
public class OSCameraControl : MonoBehaviour {
public Transform target;
public float walkDistance = 4;
public float runDistance = 4;
public float height = 2;
public float xMouseSpeed = 250.0f;
public float yMouseSpeed = 150.0f;
public float rotationDamping = 3.0f;
public float heightDamping = 2.0f;
private Transform _myTransform;
private float _x;
private float _y;
private bool _camButtonDown = false;
void Awake () {
_myTransform = transform;
}
// Use this for initialization
void Start () {
if(target == null) Debug.Log("No Target...");
else CameraSetup();
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(1)){
_camButtonDown = true;
}
if(Input.GetMouseButtonUp(1)){
_camButtonDown = false;
}
}
// этот метод вызывается после всех остальных. Для того чтобы камера...
void LateUpdate(){
if(target != null){
if(_camButtonDown){
Debug.Log("Right Mouse button Down");
_x += (Input.GetAxis("Mouse X") * xMouseSpeed * 0.02f);
_y -= (Input.GetAxis("Mouse Y") * yMouseSpeed * 0.02f);
Quaternion rotation = Quaternion.Euler(_y, _x, 0);
Vector3 position = rotation * new Vector3(0.0f, height, -walkDistance) + target.position;
_myTransform.rotation = rotation;
_myTransform.position = position;
}
else{
_x = 0;
_y = 0;
float wantedRotationAngle = _myTransform.eulerAngles.y;
float wantedHeight = _myTransform.position.y;
float currentRotationAngle = _myTransform.eulerAngles.y;
float сurrentHeight = _myTransform.position.y;
// Смягчение по оси X
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Смягчение смещения по высоте
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Преобразование угла в поворот
Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
// Установка камеры в плоскости и разворот ее на цель
_myTransform.position = target.position;
_myTransform.position -= currentRotation * Vector3.forward * walkDistance;
// Установка высоты камеры
_myTransform.position = new Vector3(_myTransform.position.x,
currentHeight,
_myTransform.position.z);
//Всегда смотреть на цель
_myTransform.LookAt (target);
}
}
}
public void CameraSetup(){
_myTransform.position = new Vector3(target.position.x,
target.position.y + height,
target.position.z - walkDistance);
_myTransform.LookAt(target);
}
}
using System.Collections;
public class OSCameraControl : MonoBehaviour {
public Transform target;
public float walkDistance = 4;
public float runDistance = 4;
public float height = 2;
public float xMouseSpeed = 250.0f;
public float yMouseSpeed = 150.0f;
public float rotationDamping = 3.0f;
public float heightDamping = 2.0f;
private Transform _myTransform;
private float _x;
private float _y;
private bool _camButtonDown = false;
void Awake () {
_myTransform = transform;
}
// Use this for initialization
void Start () {
if(target == null) Debug.Log("No Target...");
else CameraSetup();
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(1)){
_camButtonDown = true;
}
if(Input.GetMouseButtonUp(1)){
_camButtonDown = false;
}
}
// этот метод вызывается после всех остальных. Для того чтобы камера...
void LateUpdate(){
if(target != null){
if(_camButtonDown){
Debug.Log("Right Mouse button Down");
_x += (Input.GetAxis("Mouse X") * xMouseSpeed * 0.02f);
_y -= (Input.GetAxis("Mouse Y") * yMouseSpeed * 0.02f);
Quaternion rotation = Quaternion.Euler(_y, _x, 0);
Vector3 position = rotation * new Vector3(0.0f, height, -walkDistance) + target.position;
_myTransform.rotation = rotation;
_myTransform.position = position;
}
else{
_x = 0;
_y = 0;
float wantedRotationAngle = _myTransform.eulerAngles.y;
float wantedHeight = _myTransform.position.y;
float currentRotationAngle = _myTransform.eulerAngles.y;
float сurrentHeight = _myTransform.position.y;
// Смягчение по оси X
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Смягчение смещения по высоте
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Преобразование угла в поворот
Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
// Установка камеры в плоскости и разворот ее на цель
_myTransform.position = target.position;
_myTransform.position -= currentRotation * Vector3.forward * walkDistance;
// Установка высоты камеры
_myTransform.position = new Vector3(_myTransform.position.x,
currentHeight,
_myTransform.position.z);
//Всегда смотреть на цель
_myTransform.LookAt (target);
}
}
}
public void CameraSetup(){
_myTransform.position = new Vector3(target.position.x,
target.position.y + height,
target.position.z - walkDistance);
_myTransform.LookAt(target);
}
}
Скриншот ошибок: