The position of the camera and the main character remains unchanged

Camera follow script

  The camera always keeps a fixed distance from the player

// An highlighted blockusing 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

private Transform target;//定义目标的transform组件
private Vector offset;//相机跟目标的距离

void Start()
{
    
    
	target = GameObject.Find("Player").transform;
	//获取脚本激活时摄像机与Player的位置向量
	offset = gameObject.transform.position - target.position;
}
void Update()
{
    
    
	transform.position = offset + target.position;
	//以Player为起点,加上offset即为相机的位置
}

Guess you like

Origin blog.csdn.net/weixin_44870508/article/details/102805982