unity 2d camera 跟踪角色 加上背景大小限制移动

public Vector2 margin, Smoothing;
public BoxCollider2D bounds;//2d背景物理box
Vector3 min, max;
public Transform player;

Camera camera;

void Start () {

    min = bounds.bounds.min;
    max = bounds.bounds.max;
    camera = GetComponent<Camera>();

}

// Update is called once per frame
void Update () {

    var x = transform.position.x;
    var y = transform.position.y;

    if (Mathf.Abs(x-player.position.x)>margin.x)
    {
        x = Mathf.Lerp(x, player.position.x, Smoothing.x * Time.deltaTime);

    }

    if (Mathf.Abs(y-player.position.y)>margin.y)
    {
        y = Mathf.Lerp(y, player.position.y, Smoothing.y * Time.deltaTime);

    }


    var halfwidth = camera.orthographicSize * ((float) Screen.width / Screen.height);
    x = Mathf.Clamp(x, min.x + halfwidth, max.x - halfwidth);
    y = Mathf.Clamp(y, min.y + camera.orthographicSize, max.y - camera.orthographicSize);
    transform.position = new Vector3(x, y, transform.position.z);




}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 角色控制是游戏设计中必不可少的一个设计环节,这一节我们讲一讲如何制作基本的角色运动控制交互逻辑。 因为是简单实例教...
    shimmery阅读 13,233评论 5 20
  • 文/ 子茗 2015年的风就这样在我们的人生里最后一次吹去满地尘埃,你悲伤也好,欢喜也罢,2016年来了。也许我不...
    简朴斋阅读 2,915评论 0 0
  • 记忆中我和弟弟坐着拖拉机离开家乡,后面拖着行李,不知道要去哪里,那时候的我才4-5岁的样子,已经记不太清楚了,只觉...
    最美的菡阅读 1,450评论 0 0
  • 第一次看见他的照片,被吓了一跳。我以为能写出"面朝大海,春暖花开"的诗人,应该是一个朦胧派里白净忧郁的小生。后来我...
    AP就是AP阅读 3,728评论 0 0
  • 姓名:赵丽萍 公司:宁波大发化纤有限公司 组别:第264期努力二组 【日精进打卡第82天】 【知~学习】 《六项精...
    zhaoliping阅读 1,054评论 0 0

友情链接更多精彩内容