资讯 小学 初中 高中 语言 会计职称 学历提升 法考 计算机考试 医护考试 建工考试 教育百科
栏目分类:
子分类:
返回
空麓网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
空麓网 > 计算机考试 > 软件开发 > 游戏开发 > 其他

Unity2d Unity2020.3人物移动,摄像机按需跟随,并限制在背景图片内部

其他 更新时间: 发布时间: 计算机考试归档 最新发布

Unity2d Unity2020.3人物移动,摄像机按需跟随,并限制在背景图片内部

基于2020.3

        最近想实现2d游戏摄像机跟随人物移动的方法

        一开始看了b站的教程,一种方法是在窗口->包管理器->unity注册表->安装Cinemachine,安装后创建一个Cinemachine2d,并且让其flow主角,就可以实现摄像头一直跟随主角

        但是我想实现摄像头不出背景图片,并没有区深究Cinemachine这个插件,于是乎查到了一种方法,虽然不能完全实现,但经过自己的修改,已经可以勉强实现摄像头在地图内跟随,可能有不足,希望指导改进.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Camera))]
public class cama : MonoBehaviour
{

	// 我的背景图片为关于 y 轴对称的
	// 摄像机的延迟时间
	public float smoothTime = 0.2f;
	// 背景图片
	public SpriteRenderer bgBounds;
	// 角色
	public Transform target;
	// 用于缓冲摄像机
	private Vector3 velocity = Vector3.one;
	// 摄像机
	private Camera mainCamera;
	// 背景图片的宽度
	private float bgWidth;
	// 背景图片的宽度
	private float bgHeight;

    private void Start()
    {
		// 拿到摄像机
		mainCamera = GetComponent();
		// 获取背景图片宽度
		bgWidth = bgBounds.sprite.bounds.size.x * bgBounds.transform.localScale.x;
		// 获取背景图片宽度
		bgHeight = bgBounds.sprite.bounds.size.y * bgBounds.transform.localScale.y;
	}
	// 比 Update 慢一帧执行
	void LateUpdate()
	{
		// 摄像机的一半宽度
		float hight = mainCamera.orthographicSize;
		// 摄像机的一半高度,会随分辨率的宽高比成正比
		// 用高度乘以分辨率的宽高比得到宽度
		float width = hight * Screen.width / Screen.height;
		// 要移动到的位置
		Vector3 temp;
        // 当角色的横坐标 + 摄像机的一半宽度大于背景图片的一半宽度,则为到了边界,把摄像机的宽度设为临界点
        //if (width + Mathf.Abs(target.position.x) > bgWidth / 2)
        if ((width + Mathf.Abs(target.position.x) > bgWidth))
        {
            temp = new Vector3(Mathf.Sign(target.position.x) * (bgWidth - width), target.position.y, transform.position.z);
        }
        else
        {
            // 否则则为正常移动
            temp = new Vector3(target.position.x, target.position.y, transform.position.z);
        }
		if ((hight + Mathf.Abs(target.position.y) > bgHeight))
		{
			temp = new Vector3(temp.x, Mathf.Sign(target.position.y) * (bgHeight - hight), transform.position.z);
		}
		else
		{
			// 否则则为正常移动
			temp = new Vector3(temp.x, target.position.y, transform.position.z);
		}

		Debug.Log("position,x:"+bgBounds.transform.position.x+",y:"+bgBounds.transform.position.y);
		//添加临界
		//图片边界
        //小于图片的底边
        //很好解释,拦截一下摄像机的位置,如果到这个界限就让他等于这个界限
        //界限就是先算出背景图片的上下左右边的位置,让摄像机的位置等于这个位置并且加上自己的上下左右的一半长度
        //x的此处加减2是因为有误差,根据视图调节的
		temp.y = temp.y <= bgBounds.transform.position.y - bgHeight / 2+ hight ? bgBounds.transform.position.y - bgHeight / 2+ hight : temp.y;
		temp.y = temp.y >= bgBounds.transform.position.y + bgHeight / 2- hight ? bgBounds.transform.position.y + bgHeight / 2- hight : temp.y;
		temp.x = temp.x <= bgBounds.transform.position.x - bgWidth / 2+width ? bgBounds.transform.position.x - bgWidth / 2 + width-2 : temp.x;
		temp.x = temp.x >= bgBounds.transform.position.x + bgWidth / 2 - width ? bgBounds.transform.position.x + bgWidth / 2-width+2 : temp.x;
    // 实现摄像机的延迟移动

		transform.position = Vector3.SmoothDamp(transform.position, temp, ref velocity, smoothTime);

		
	}
}

代码是直接挂在摄像机上,并且把需要的背景,和人物需要在unity拖进去,部分代码借鉴于hellolxb

转载请注明:文章转载自 http://www.konglu.com/
本文地址:http://www.konglu.com/it/901789.html
免责声明:

我们致力于保护作者版权,注重分享,被刊用文章【Unity2d Unity2020.3人物移动,摄像机按需跟随,并限制在背景图片内部】因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理,本文部分文字与图片资源来自于网络,转载此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请立即通知我们,情况属实,我们会第一时间予以删除,并同时向您表示歉意,谢谢!

我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2023 成都空麓科技有限公司

ICP备案号:蜀ICP备2023000828号-2