Unity 实战记录摄像机区域内跟随物体

Posted jqiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity 实战记录摄像机区域内跟随物体相关的知识,希望对你有一定的参考价值。

模仿Cinemachined 效果,用Cinemachine可轻松实现,贴代码

  1 using System.Collections;
  2 using System.Collections.Generic;
  3 using UnityEngine;
  4 
  5 public class FiveCameraController : MonoBehaviour
  6 {
  7 
  8 
  9     public Transform targetPlayer;
 10 
 11     private Vector3 centerPos = Vector3.zero;
 12     private float borderRadius = 27f;
 13     private float moveRadius = 0.4f;
 14 
 15     private bool isInitPos = false;
 16     private Camera camera1;
 17     // Use this for initialization
 18     void Start()
 19     {
 20         //相机为正交模式
 21         camera1 = GetComponent<Camera>();
 22         camera1.orthographicSize = 8;
 23         borderRadius -= camera1.orthographicSize;
 24         moveRadius *= camera1.orthographicSize;
 25     }
 26 
 27     // Update is called once per frame
 28     void Update()
 29     {
 30         if (targetPlayer == null)
 31         {
 32             return;
 33         }
 34         if (!isInitPos)
 35         {
 36             
 37             transform.position = setPosInArea(targetPlayer.position);
 38             isInitPos = true;
 39         }
 40 
 41         Vector3 offset = getMoveOffset(targetPlayer.position);
 42         if (Vector3.zero != offset)
 43         {
 44             transform.position += offset;
 45             transform.position = setPosInArea(transform.position);
 46            
 47         }
 48         
 49     }
 50     /// <summary>
 51     /// 区域限制
 52     /// </summary>
 53     /// <param name="pos"></param>
 54     /// <returns></returns>
 55     public Vector3 setPosInArea(Vector3 pos)
 56     {
 57 
 58         if (pos.x > centerPos.x && pos.x > centerPos.x + borderRadius)
 59         {
 60             pos.x = centerPos.x + borderRadius;
 61         }
 62         if (pos.x < centerPos.x && pos.x < centerPos.x - borderRadius)
 63         {
 64             pos.x = centerPos.x - borderRadius;
 65         }
 66         if (pos.y > centerPos.y && pos.y > centerPos.y + borderRadius)
 67         {
 68             pos.y = centerPos.y + borderRadius;
 69         }
 70         if (pos.y < centerPos.y && pos.y < centerPos.y - borderRadius)
 71         {
 72             pos.y = centerPos.y - borderRadius;
 73         }
 74 
 75         return pos;
 76     }
 77     /// <summary>
 78     /// 得到移动插值
 79     /// </summary>
 80     /// <param name="pos"></param>
 81     /// <returns></returns>
 82     public Vector3 getMoveOffset(Vector3 pos)
 83     {
 84         Vector3 cameraPos = transform.position;
 85         Vector3 offset = Vector3.zero;
 86         if (pos.x > cameraPos.x && pos.x > cameraPos.x + moveRadius)
 87         {
 88             offset.x = pos.x - (cameraPos.x + moveRadius);
 89         }
 90         if (pos.x < cameraPos.x && pos.x < cameraPos.x - moveRadius)
 91         {
 92             offset.x = pos.x - (cameraPos.x - moveRadius);
 93         }
 94         if (pos.y > cameraPos.y && pos.y > cameraPos.y + moveRadius)
 95         {
 96             offset.y = pos.y - (cameraPos.y + moveRadius);
 97         }
 98         if (pos.y < cameraPos.y && pos.y < cameraPos.y - moveRadius)
 99         {
100             offset.y = pos.y - (cameraPos.y - moveRadius);
101         }
102         return offset;
103 
104     }
105     public void SetTargetPlayer(Transform player)
106     {
107         this.targetPlayer = player;
108 
109 
110     }
111 }

效果:

技术分享图片

以上是关于Unity 实战记录摄像机区域内跟随物体的主要内容,如果未能解决你的问题,请参考以下文章

Unity摄像机跟随的几种方案

unity 相机跟随物体(角色)

unity3d里照相机跟随鼠标左右移动的代码怎么写

Unity实现人物移动和镜头跟随

怎么用unity3d模拟吃鸡中的摄像机跟随

Unity 中简单的第三人称摄像机跟随