初学者:做第一人称视角。
首先得注意摄像机的挂载,玩家与摄像机的绑定,应该将摄像机绑定在玩家身上,通过移动鼠标去转动视角
float z = Input.GetAxis("Mouse ScrollWheel"); 鼠标滚轮按键
Camera.main.fieldOfView鼠标滚轮参数,值越大能放大的效果学校,值越小放大的效果越明显
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseWheel : MonoBehaviour
{
private float speed = 50;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Mouse X");
float y = Input.GetAxis("Mouse Y");
float z = Input.GetAxis("Mouse ScrollWheel"); //鼠标滚轮
//鼠标滚轮前推
if (z<0)
{
//摄像机的视角值
if (Camera.main.fieldOfView<90)
{
//缩小
Camera.main.fieldOfView += 2;
}
}
//鼠标滚轮后推
if (z > 0)
{
//摄像机的视角值
if (Camera.main.fieldOfView > 30)
{
//放大
Camera.main.fieldOfView -= 2;
}
}
//是否移动水平轴
if (Mathf.Abs(x) != 0 || Mathf.Abs(y) != 0)
{
x *= Time.deltaTime * speed;
transform.Rotate(0, x, 0);
y *= Time.deltaTime * speed;
transform.Rotate(-y, 0, 0);
}
}
}