创建脚本文件夹
创建脚本
编写代码:CameraCtrl.cs
using UnityEngine;
public class CameraCtrl : MonoBehaviour{
float dis;
float height;
float speed;
Transform target;
Vector3 targetPos;
void Awake(){
Screen.SetResolution(2304, 1296, FullScreenMode.Windowed);
}
void Start(){
dis = 5;
height = 3;
speed = 0.06f;
SetTarget();
}
void Update(){
transform.LookAt(target.position + Vector3.up * 1.5f);
targetPos = target.forward * (-dis) + target.up * height + target.position;
}
void LateUpdate(){
transform.position = Vector3.Lerp(transform.position, targetPos, speed);
}
void SetTarget(){
Transform player = GameObject.FindGameObjectWithTag("Player").transform;
if (player != null && target == null)
target = player;
}
}
修改客户端代码:MainCitySys.cs
运行服务端
运行客户端 - 摄像机已跟随主角
本章结束