unity代码对摄像机转向控制练习
Posted qing__sheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity代码对摄像机转向控制练习相关的知识,希望对你有一定的参考价值。
摄像机转向跟随练习,主要为对现有的代码进行模拟练习
对摄像机的控制
设计人物与对象之间保持一定的距离关系,并且可以控制镜头的缩放与转向,使其能够更加适应运行的人物视角转换。
代码首先需要声明的对象
private Transform player;//记录玩家的位置
private Vector3 offsetPosition;//记录位置的偏移量
private bool isRotate;//判断鼠标右键是否按下,是否转换视角
public float distance;//记录距离
public float scrollSpeed = 10;//视野远近变化的快慢
public float RotateSpeed = 2;//点击后视角转变的速度
start函数内部
寻找tag为Player的对象,然后通过声明,将他的数据附在player上。下一步通过LookAt函数,将视角锁定对向player的位置,并找到它的位置。之前所声明的offsetPosition,也就是偏移量,用来得出原来的位置和现有的位置的差距。
void Start()
player = GameObject.FindGameObjectWithTag("Player").transform;
transform.LookAt(player.transform.position);
offsetPosition = transform.position - player.position;
鼠标滑轮控制视野的拉伸
magnitude返回偏移距离的向量长度给distance,所产生的偏移距离的改变量,取决于收到的鼠标滚轮的幅度乘上之前设定的滚动速度。利用Mathf.Clamp来设定最远和最近的距离,超过这个限制的值将不会发生改变。重新更改偏移量,通过normalized将其单位话再乘上distance。
void ScrollView()
//通过鼠标滑轮拉近远视野
distance = offsetPosition.magnitude;
distance -= Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
distance = Mathf.Clamp(distance, 2, 18);
offsetPosition = offsetPosition.normalized * distance;
鼠标右键控制方向转动
通过判断鼠标右键是否摁下,对bool值isRotate判断是否为true或者false,如果是摁下,则是视角需要进行旋转。通过RotateAround函数判断player的位置,并且返回player的向上矢量,转变的角度则是速度乘以鼠标改变的方向判定(range 为-1到1)。
void RotateView()
if (Input.GetMouseButtonDown(1))
isRotate = true;
if (Input.GetMouseButtonUp(1))
isRotate = false;
if (isRotate)
transform.RotateAround(player.position,player.up,RotateSpeed * Input.GetAxis("Mouse X"));
Vector3 originalPos = transform.position;
Quaternion originalRotation = transform.rotation;
transform.RotateAround(player.position,transform.right,-RotateSpeed * Input.GetAxis("Mouse Y"));
float x = transform.eulerAngles.x;
if (x < 10 || x > 80)
transform.position = originalPos;
transform.rotation = originalRotation;
update部分的调用
控制更改的位置为偏移量与原坐标的相加
注意RotateView()和ScrollView()函数位置不要替换,否则缩放的函数无法调用
void Update()
transform.position = offsetPosition + player.position;
//更新相机的位置
RotateView();
ScrollView();
全部代码
去博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片
.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using UnityEngine;
public class Camerafollow : MonoBehaviour
private Transform player;//记录玩家的位置
private Vector3 offsetPosition;//记录位置的偏移量
private bool isRotate;//判断鼠标右键是否按下,是否转换视角
public float distance;//记录距离
public float scrollSpeed = 10;//视野远近变化的快慢
public float RotateSpeed = 2;//点击后视角转变的速度
// Start is called before the first frame update
void Start()
player = GameObject.FindGameObjectWithTag("Player").transform;
transform.LookAt(player.transform.position);
offsetPosition = transform.position - player.position;
// Update is called once per frame
void Update()
transform.position = offsetPosition + player.position;
//更新相机的位置
RotateView();
ScrollView();
void ScrollView()
//通过鼠标滑轮拉近远视野
distance = offsetPosition.magnitude;
distance -= Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
distance = Mathf.Clamp(distance, 2, 18);
offsetPosition = offsetPosition.normalized * distance;
void RotateView()
if (Input.GetMouseButtonDown(1))
isRotate = true;
if (Input.GetMouseButtonUp(1))
isRotate = false;
if (isRotate)
transform.RotateAround(player.position,player.up,RotateSpeed * Input.GetAxis("Mouse X"));
Vector3 originalPos = transform.position;
Quaternion originalRotation = transform.rotation;
transform.RotateAround(player.position,transform.right,-RotateSpeed * Input.GetAxis("Mouse Y"));
float x = transform.eulerAngles.x;
if (x < 10 || x > 80)
transform.position = originalPos;
transform.rotation = originalRotation;
offsetPosition = transform.position - player.position;
代码源
链接: link.
小松教你手游开发unity实用技能控制摄像头脚本
测试项目的时候经常用到的控制摄像头运动,简单的移动转向using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour {
private GameObject gameObject;
float x1;
float x2;
float x3;
float x4;
void Start()
{
gameObject = GameObject.Find("Main Camera");
// Make the rigid body not change rotation
if (GetComponent<Rigidbody>())
GetComponent<Rigidbody>().freezeRotation = true;
}
// Update is called once per frame
void Update()
{
//空格键抬升高度
if (Input.GetKey(KeyCode.Space))
{
transform.position = new Vector3(transform.position.x, transform.position.y + 1, transform.position.z);
}
//w键前进
if (Input.GetKey(KeyCode.W))
{
this.gameObject.transform.Translate(new Vector3(0, 0, 50 * Time.deltaTime));
}
//s键后退
if (Input.GetKey(KeyCode.S))
{
this.gameObject.transform.Translate(new Vector3(0, 0, -50 * Time.deltaTime));
}
//a键后退
if (Input.GetKey(KeyCode.A))
{
this.gameObject.transform.Translate(new Vector3(-10, 0, 0 * Time.deltaTime));
}
//d键后退
if (Input.GetKey(KeyCode.D))
{
this.gameObject.transform.Translate(new Vector3(10, 0, 0 * Time.deltaTime));
}
if(Input.GetKey(KeyCode.Mouse0))
{
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
}
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
}
以上是关于unity代码对摄像机转向控制练习的主要内容,如果未能解决你的问题,请参考以下文章