视频:bilibili@绯之炭 这种第三人称镜头自由舒适,被许多大型游戏采用。当然视频中没有实现太多细节,实际应用还需要大幅度改进。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class f_mc : MonoBehaviour
{
public Transform player;
public GameObject asso;
public float spinSpeed = 512;
public float scale_mul = 128;
public float Yaxis_mul=0.02f;
Vector3 setoff;
void Start()
{
Cursor.visible = false;
setoff = transform.position - player.position;
}
void Update()
{
float scale = Input.GetAxis("Mouse ScrollWheel");
if (scale > 0 && Camera.main.fieldOfView < 100 || scale < 0 && Camera.main.fieldOfView > 50)
{
Camera.main.fieldOfView += scale * scale_mul;
}
float X = Input.GetAxis("Mouse X");
Quaternion spin = Quaternion.AngleAxis(X * spinSpeed * Time.deltaTime, player.up);
transform.position = spin * setoff + player.position;
float Y = Input.GetAxis("Mouse Y");
float Yincrement = -Y * Yaxis_mul;
if (Y > 0 && transform.position.y + Yincrement > 16.8 || Y < 0 && transform.position.y + Yincrement < 21.2)
{
transform.position += new Vector3(0, Yincrement, 0);
}
setoff = transform.position - player.position;
transform.LookAt(asso.transform.position);
}
}
|