• 太空大战背景移动的几种方式


    1.

    //两张背景分开移动
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Background : MonoBehaviour {
        public float speed =1;//背景移动速度
       public float time1 = 0;
        public float startime=5;
        public GameObject bj1;
        public GameObject bj2;
    
        Vector3 vec1;
        Vector3 vec2;
        // Use this for initialization
        void Start()
        {
            vec1 = bj1.transform.position;
            vec2 = bj2.transform.position;
    
            // Update is called once per frame
        }
            void Update()
            {
                time1 += Time.deltaTime;
                bj1.transform.Translate(new Vector3(0, 0, -0.02f * speed),Space.World);
            bj2.transform.Translate(new Vector3(0, 0, -0.02f * speed),Space.World);
    
            if (bj1.transform.position.z < -20)
            {
                bj1.transform.position = vec2;
            }
            if (bj2.transform.position.z < -20)
            {
                bj2.transform.position = vec2;
            }
            }
        } 

    2.

    两张背景图一起移动
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class back02 : MonoBehaviour {
        public float speed = 2;
        // Use this for initialization
        void Start () {
            
        }
        
        // Update is called once per frame
        void Update () {
            transform.Translate(new Vector3(0, 0, -0.02f*speed), Space.World);
            if (transform.position.z < 0)
            {
                transform.position = new Vector3(0, -5, 20);
            }
        }
    }

    3.材质移动

     public float speed = 0.25f;
        // Use this for initialization
        void Start () {
           // pos = transform.position;
        }
        
        // Update is called once per frame
        void Update () {
    
            float move = Time.time * speed;
            GetComponent<MeshRenderer>().material.mainTextureOffset = new Vector2(0,move);
            
        }

    4.用MoveTowards

    using UnityEngine;
    using System.Collections;
    
    public class GameGround : MonoBehaviour {
        public GameObject[] map;
        public Vector3 endsence ;
        public Vector3 startmountion;
        // Use this for initialization
        void Start () {
    
        }
        
        // Update is called once per frame
        void Update () {
                    movescene (map, endsence, 0.5f);
    }
        void movescene(GameObject[] scene,Vector3 endsence,float speed)
        {
            foreach(GameObject mysence in scene){
                if(mysence!=null)
                {
                    float dY=mysence.transform.localPosition.y-endsence.y;
                    if(dY<0){
                        print (1);
                        mysence.transform.localPosition=startmountion+new Vector3(dY+0.05f,0,0);
                    }
                    mysence.transform.localPosition= Vector3.MoveTowards (mysence.transform.localPosition, new Vector3(0,-200f,0), Time.deltaTime *speed);
                }
            }
    
        }
    
    }
    莫说我穷的叮当响,大袖揽清风。 莫讥我困时无处眠,天地做床被。 莫笑我渴时无美酒,江湖来做壶。
  • 相关阅读:
    【FPGA篇章四】FPGA状态机:三段式以及书写方法
    【FPGA篇章三】FPGA常用语句:Verilog基本语法要素
    【FPGA篇章二】FPGA开发流程:详述每一环节的物理含义和实现目标
    【FPGA篇章一】FPGA工作原理:详细介绍FPGA实现编程逻辑的机理
    学习python随笔记
    Spring中的@Bean注解、@Configuration注解、@Value
    什么是Maven项目
    SpringBoot(四)thymeleaf+MyBatis+MySql
    SpringBoot(三)thymeleaf+JPA+MySql
    SpringBoot(二)thymeleaf模板的引入
  • 原文地址:https://www.cnblogs.com/huang--wei/p/9609459.html
Copyright © 2020-2023  润新知