zoukankan      html  css  js  c++  java
  • Unity3D 相机跟随主角移动

    这里给主相机绑定一个脚本。

    脚本写为:

    using UnityEngine;
    using System.Collections;
    
    public class camerafollow : MonoBehaviour {
        //主摄像机跟随主角一起移动
        public float xMargin = 1f;
        public float yMargin = 1f;
        public float xSmooth = 8f;
        public float ySmooth = 8f;
        public Vector2 maxXandY;
        public Vector2 minXandY;
    	// Use this for initialization
        private Transform player;
    	void Start () {
    //这里获得是绑定的主角,需要一起跟随移动,就要获得主角的属性 player = GameObject.FindGameObjectWithTag("pk_0").transform; maxXandY.x = 10; maxXandY.y = 10; } bool checkxmargin() { return Mathf.Abs(transform.position.x - player.position.x) > xMargin; } bool checkymargin() { return Mathf.Abs(transform.position.y - player.position.y) > yMargin; } // Update is called once per frame void Update () { Trackplayer(); } //跟踪主角 void Trackplayer() { float targetx = transform.position.x; float targety = transform.position.y; if (checkxmargin()) { targetx = Mathf.Lerp(transform.position.x, player.position.x, xSmooth * Time.deltaTime); } if (checkymargin()) { targety = Mathf.Lerp(transform.position.y, player.position.y, xSmooth * Time.deltaTime); } targetx = Mathf.Clamp(targetx, minXandY.x, maxXandY.y); targety = Mathf.Clamp(targety, minXandY.y, maxXandY.y); transform.position = new Vector3(targetx, targety,transform.position.z); } }

      效果图:

  • 相关阅读:
    还记得吗
    PAT A 1065. A+B and C (64bit) (20)
    畅通project(杭电1863)
    cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第七步---英雄要升级&属性--解析csv配置文件
    热烈祝贺Polymer中文组织站点上线
    具体解释HTML中的window对象和document对象
    oc15--文档安装
    oc14--匿名对象
    oc13--pragma mark
    oc12--对象作为参数
  • 原文地址:https://www.cnblogs.com/sunxun/p/4934531.html
Copyright © 2011-2022 走看看