unity 攝像機的控制
阿新 • • 發佈:2019-02-12
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class CameraMove : MonoBehaviour
{
void Start()
{
this.gameObject.transform.DOMove(new Vector3(-3.35f, 5.8f, -4.4f), 1);
this.gameObject.transform.DORotate(new Vector3(44, 20, 0), 1);
}
void Update()
{
//左右移動
float xx;
float yy;
xx = this.gameObject.transform.position.x;
yy = this.gameObject.transform.position.y;
if (xx > -5)
{
if (Input.GetKey(KeyCode.LeftArrow))
{
this.gameObject.transform.position -= new Vector3(0.05f, 0, 0);
}
}
if (xx < 5)
{
if (Input.GetKey(KeyCode.RightArrow))
{
this.gameObject.transform.position += new Vector3(0.05f, 0, 0);
}
}
//放大縮小
float size;
size = Camera.main.fieldOfView;
if (Input.GetKey(KeyCode.DownArrow))
{
Camera.main.fieldOfView = size + 1;
if (Camera.main.fieldOfView >= 60)
{
Camera.main.fieldOfView = 60;
}
}
if (Input.GetKey(KeyCode.UpArrow))
{
Camera.main.fieldOfView = size - 1;
if (Camera.main.fieldOfView <= 10)
{
Camera.main.fieldOfView = 10;
}
}
// 鏡頭左右旋轉
float rotateX;
float rotateY;
if (Input.GetMouseButton(1))
{
rotateX = -Input.GetAxis("Mouse X");
rotateY = Input.GetAxis("Mouse Y");
}
else
{
rotateX = 0;
rotateY = 0;
}
this.gameObject.transform.Rotate(new Vector3(0, rotateX, 0), Space.World);
//鏡頭上下旋轉
float rx = 0;
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (yy >= 1)
{
rx = rx - 1f;
this.gameObject.transform.Rotate(rx, 0, 0);
this.gameObject.transform.position -= new Vector3(0, 0.1f, 0);
}
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (yy <= 7.5)
{
rx = rx + 1f;
this.gameObject.transform.Rotate(rx, 0, 0);
this.gameObject.transform.position += new Vector3(0, 0.1f, 0);
}
}
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class CameraMove : MonoBehaviour
{
void Start()
{
this.gameObject.transform.DOMove(new Vector3(-3.35f, 5.8f, -4.4f), 1);
this.gameObject.transform.DORotate(new Vector3(44, 20, 0), 1);
}
void Update()
{
//左右移動
float xx;
float yy;
xx = this.gameObject.transform.position.x;
yy = this.gameObject.transform.position.y;
if (xx > -5)
{
if (Input.GetKey(KeyCode.LeftArrow))
{
this.gameObject.transform.position -= new Vector3(0.05f, 0, 0);
}
}
if (xx < 5)
{
if (Input.GetKey(KeyCode.RightArrow))
{
this.gameObject.transform.position += new Vector3(0.05f, 0, 0);
}
}
//放大縮小
float size;
size = Camera.main.fieldOfView;
if (Input.GetKey(KeyCode.DownArrow))
{
Camera.main.fieldOfView = size + 1;
if (Camera.main.fieldOfView >= 60)
{
Camera.main.fieldOfView = 60;
}
}
if (Input.GetKey(KeyCode.UpArrow))
{
Camera.main.fieldOfView = size - 1;
if (Camera.main.fieldOfView <= 10)
{
Camera.main.fieldOfView = 10;
}
}
// 鏡頭左右旋轉
float rotateX;
float rotateY;
if (Input.GetMouseButton(1))
{
rotateX = -Input.GetAxis("Mouse X");
rotateY = Input.GetAxis("Mouse Y");
}
else
{
rotateX = 0;
rotateY = 0;
}
this.gameObject.transform.Rotate(new Vector3(0, rotateX, 0), Space.World);
//鏡頭上下旋轉
float rx = 0;
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (yy >= 1)
{
rx = rx - 1f;
this.gameObject.transform.Rotate(rx, 0, 0);
this.gameObject.transform.position -= new Vector3(0, 0.1f, 0);
}
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (yy <= 7.5)
{
rx = rx + 1f;
this.gameObject.transform.Rotate(rx, 0, 0);
this.gameObject.transform.position += new Vector3(0, 0.1f, 0);
}
}