zoukankan      html  css  js  c++  java
  • 2D游戏摄像机跟随不出界

    做2D游戏的时候碰上摄像机要跟随人物时要防止摄像机看到界面外的黑幕,直接上代码。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    /// <summary>
    /// 控制在老师身上摄像机的位置,防止看到场景外的黑幕
    /// </summary>
    public class objectCameraLimit : MonoBehaviour {
        public float[] cameraLimitRange;//分别为上 下 左 右
        public Transform objectTrans;//老师
        // Use this for initialization
        void Start () {
            objectTrans = transform.parent;
        }
        
        // Update is called once per frame
        void Update () {
            cameraLimit();
        }
        void cameraLimit()
        {
            if(objectTrans.localPosition.y > cameraLimitRange[0])
            {
                transform.localPosition = new Vector3(transform.localPosition.x,cameraLimitRange[0] - objectTrans.localPosition.y,transform.localPosition.z);
            }
            else if(objectTrans.localPosition.y < cameraLimitRange[1])
            {
                transform.localPosition = new Vector3(transform.localPosition.x,cameraLimitRange[1] - objectTrans.localPosition.y,transform.localPosition.z);
            }
    
            if (objectTrans.localPosition.x > cameraLimitRange[3])
            {
                transform.localPosition = new Vector3(cameraLimitRange[3] - objectTrans.localPosition.x, transform.localPosition.y, transform.localPosition.z);
            }
            else if (objectTrans.localPosition.x < cameraLimitRange[2])
            {
                transform.localPosition = new Vector3(cameraLimitRange[2] - objectTrans.localPosition.x, transform.localPosition.y, transform.localPosition.z);
            }
        }
    }
  • 相关阅读:
    vue.js 系列教程
    vue.js 生命周期
    MVVM代码例子
    vue.js windows下开发环境搭建
    Vue.js 之修饰符详解
    elementUi——适合于Vue的UI框架
    Vue.js——60分钟快速入门
    Keil sct分散加载文件
    Keil ARM-CM3 printf输出调试信息到Debug (printf) Viewer
    Cortex-M3(NXP LPC 1788) 启动代码
  • 原文地址:https://www.cnblogs.com/dsh20134584/p/7422696.html
Copyright © 2011-2022 走看看