攝像機跟隨物體方法一是把攝像機設定為物體Player的子物體,
話不多說,先把影片演示放出來康康,

接下來是,具體的原始碼:
一、在Main Camera下創建一個腳本FollowPlayer ,將下面的原始碼復制進去,
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
// //定義一個Transform型別的player
private Transform player;
//位置偏移(攝像機與人)
private Vector3 offsetPosition;
// public float distance = 0;
// public float scrollspeed = 1;
//在Start里獲取到移動物體Player的transform組件
void Start()
{
//攝像機跟隨角色移動
//得到組件,先是給Player設定個Tag,當然也可以用Find來找Player名的方式,下面;但是不建議使用,
// player = GameObject.Find("Player").transform;
player = GameObject.FindGameObjectWithTag(Tags.player).transform;
transform.LookAt(player.position);//讓攝像機面向角色位置
offsetPosition = transform.position - player.position;//得到偏移量
}
void Update()
{
//讓攝像機的位置= 人物行走的位置+與偏移量的相加
transform.position = offsetPosition + player.position;
}
二、需要給角色將Tag標簽換成Player,

接著實作攝像機(Camera)滑鼠滑輪控制畫面縮放與放大在博主的另一篇文章里,https://blog.csdn.net/qq_45872962/article/details/111281062
unity中瞄準倍鏡效果和原始碼:https://blog.csdn.net/qq_45872962/article/details/109637100
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/236121.html
標籤:其他
下一篇:后科技時代—賽博朋克2077
