zoukankan      html  css  js  c++  java
  • Unity3D_(游戏)跳一跳超简单制作过程

      

    跳一跳

      工程文件界面

      游戏界面

      

      脚本

    using DG.Tweening;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    using UnityEngine.UI;
    
    public class Player : MonoBehaviour {
    
        public float Factoer=1;
    
        public float MaxDistance = 4;
        public GameObject Stage;
    
        //获取相机位置
        public Transform Camera;
    
        private Rigidbody _rigidbody;
        private float _stateTime;
    
        public GameObject Particle;
    
        public Transform Head;
        public Transform Body;
    
        //当前盒子物体
        private GameObject _currentStage;
        private Collider _lastCollisionCollider;
    
        //相机的相对位置
        public Vector3 _camerRelativePosition;
    
        public Text ScoreText;
        private int _score;
    
        Vector3 _direction = new Vector3(1, 0, 0);
    
        // Use this for initialization
        void Start () {
            _rigidbody = GetComponent<Rigidbody>();
            Particle = GameObject.Find("yellow");
            Particle.SetActive(false);
            //修改物理组件的重心到body的底部
            _rigidbody.centerOfMass = Vector3.zero;
    
            _currentStage = Stage;
            _lastCollisionCollider = _currentStage.GetComponent<Collider>();
            SpawnStage();
    
            _camerRelativePosition = Camera.position - transform.position;
            
        }
        
        // Update is called once per frame
        void Update () {
           
            if (Input.GetKeyDown(KeyCode.Space))
            {
                _stateTime = Time.time;
                Particle.SetActive(true);
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                var elapse = Time.time - _stateTime;
                OnJump(elapse);
                Particle.SetActive(false);
    
                Body.transform.DOScale(0.1f,1);
                Head.transform.DOLocalMoveY(0.29f,0.5f);
    
                _currentStage.transform.DOLocalMoveY(0.25f,0.2f);
                _currentStage.transform.DOScale(new Vector3(1,0.5f,1),0.2f);
            }
            if (Input.GetKey(KeyCode.Space))
            {
                Body.transform.localScale += new Vector3(1, -1, 1) * 0.05f * Time.deltaTime;
                Head.transform.localPosition += new Vector3(0,-1,0)*0.1f*Time.deltaTime;
    
                //盒子缩放沿着轴心缩放
                _currentStage.transform.localScale += new Vector3(0, -1, 0)*0.15f*Time.deltaTime;
                _currentStage.transform.localPosition += new Vector3(0, -1, 0) * 0.15f * Time.deltaTime;
            }
    
        }
    
        void OnJump(float elapse)
        {
            _rigidbody.AddForce((new Vector3(0,1,0)+_direction)*elapse* Factoer,ForceMode.Impulse);
        }
    
        void SpawnStage()
        {
            var stage = Instantiate(Stage);
            stage.transform.position = _currentStage.transform.position + _direction * Random.Range(1.1f,MaxDistance) ;
    
    
            var randomScale = Random.Range(0.5f,1);
            Stage.transform.localScale = new Vector3(randomScale, 0.5f, randomScale);
    
            stage.GetComponent<Renderer>().material.color = new Color(Random.Range(0.01f, 1), Random.Range(0.01f, 1), Random.Range(0.01f, 1));
    
        }
    
        //如果有别的物体和本物体发生变化,会触发这函数
        void OnCollisionEnter(Collision collision)
        {
           if(collision.gameObject.name.Contains("Stage") && collision.collider!=_lastCollisionCollider)
            {
                _lastCollisionCollider = collision.collider;
                _currentStage = collision.gameObject;
                RandomDirection();
                SpawnStage();
                MoveCamera();
    
                _score++;
                ScoreText.text = _score.ToString();
            }
    
           if(collision.gameObject.name=="Ground")
            {
                //本局游戏结束,重新开始
                SceneManager.LoadScene("Gary");
            }
        }
    
        void RandomDirection()
        {
            var seed = Random.Range(0,2);
            if(seed == 0)
            {
                _direction = new Vector3(1, 0, 0);
            }
            else
            {
                _direction = new Vector3(0,0,1);
            }
            
        }
    
        void MoveCamera()
        {
             Camera.DOMove(transform.position + _camerRelativePosition,1);
        }
    }
    
    Player.cs
    Player.cs

      

      程序已放到Github上托管(里面有个32bit可执行文件):传送门

        小游戏没有用到素材、资源包(DOTween插件不算)

      一个脚本  

    实现过程

      【脚本中public外部引用的控件需要自己拖拽到相应位置上!!!】

    创建一个3D场景

      创建一个Cube,y轴缩放0.25当跳板,创建物体可以通过Transsform中Reset设置为3D场景正中心,在创建一个Plane当地面,跳一跳小人物碰到地面时游戏借宿,为了让跳板出现地面上方,设置y轴Postion为0.25(Reset设置居中是以组件正中心)

      创建一个材质球改变地面颜色(默认Pllane控件是不能设置材质颜色)

      创建玩家角色

      Cylinder(圆柱形)控件作为玩家身体

      Sphere(圆形)控件作为玩家头部

      添加材质球,将材质球绑定到Cylinder(圆柱形)控件和Sphere(圆形)控件上,改变材质球颜色

      保存场景

    角色跳跃

      添加游戏脚本Player,并将降本绑定到Player控件上,并在Player控件上添加一个物理组件Rigidbody

      获得组件

        private Rigidbody _rigidbody;
    
        void Start () {
            _rigidbody = GetComponent<Rigidbody>();
        }

      计算出按下空格(space)和松开空格之间的时间

       private float _stateTime;
    
        void Update () {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                _stateTime = Time.time;
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                var elapse = Time.time - _stateTime;
                OnJump(elapse);
            }
        }

      Player主键跳跃的距离

        public float Factoer=1;
    
        void OnJump(float elapse)
        {
            _rigidbody.AddForce(new Vector3(1,1,0)*elapse* Factoer,ForceMode.Impulse);
        }

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Player : MonoBehaviour {
    
        public float Factoer=1;
    
        private Rigidbody _rigidbody;
        private float _stateTime;
    
        // Use this for initialization
        void Start () {
            _rigidbody = GetComponent<Rigidbody>();
        }
        
        // Update is called once per frame
        void Update () {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                _stateTime = Time.time;
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                var elapse = Time.time - _stateTime;
                OnJump(elapse);
            }
        }
    
        void OnJump(float elapse)
        {
            _rigidbody.AddForce(new Vector3(1,1,0)*elapse* Factoer,ForceMode.Impulse);
        }
    }
    Player.cs

      发现人物跳跃落地时会出现跌倒,这是因为Body底部是圆的

      将Body下的Capsle Collider去掉,替换成Box Collider

      

      通过Player.cs修改Player

        void Start () {
            _rigidbody = GetComponent<Rigidbody>();
            //修改物理组件的重心到body的底部
            _rigidbody.centerOfMass = Vector3.zero;
        }

      

      修改当前视角,选中Main Camera按下快捷键Ctrl+Alt+F  (GameObject->Align With->View)

      修改Factoer为3,测试(这个盒子是我自己复制Stage出来的)

    盒子自动生成

       随机生成盒子位置

    stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),0,0);
        private Rigidbody _rigidbody;
        private float _stateTime;
        //当前盒子物体
        private GameObject _currentStage;
    
        void Start () {
            _rigidbody = GetComponent<Rigidbody>();
            //修改物理组件的重心到body的底部
            _rigidbody.centerOfMass = Vector3.zero;
    
            _currentStage = Stage;
            SpawnStage();
        }
    
        void SpawnStage()
        {
            var stage = Instantiate(Stage);
            stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),0,0);
        }

      

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Player : MonoBehaviour {
    
        public float Factoer=1;
    
        public float MaxDistance = 5;
        public GameObject Stage;
    
        private Rigidbody _rigidbody;
        private float _stateTime;
        //当前盒子物体
        private GameObject _currentStage;
    
        // Use this for initialization
        void Start () {
            _rigidbody = GetComponent<Rigidbody>();
            //修改物理组件的重心到body的底部
            _rigidbody.centerOfMass = Vector3.zero;
    
            _currentStage = Stage;
            SpawnStage();
        }
        
        // Update is called once per frame
        void Update () {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                _stateTime = Time.time;
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                var elapse = Time.time - _stateTime;
                OnJump(elapse);
            }
        }
    
        void OnJump(float elapse)
        {
            _rigidbody.AddForce(new Vector3(1,1,0)*elapse* Factoer,ForceMode.Impulse);
        }
    
        void SpawnStage()
        {
            var stage = Instantiate(Stage);
            stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),0,0);
        }
    }
    Player.cs

      跳到一个盒子上再随机生成一个盒子

      如果有别的物体和本物体发生变化,会触发这函数OnCollisionEnter(Collision collision)

        void OnCollisionEnter(Collision collision)
        {
           if(collision.gameObject.name.Contains("Stage") && collision.collider!=_lastCollisionCollider)
            {
                _lastCollisionCollider = collision.collider;
                _currentStage = collision.gameObject;
                SpawnStage();
            }
        }

      1、轻轻一跳在同一个盒子上时,不能在重新生成新的盒子

      2、没有跳到下一个盒子上,不能再生成新的盒子

    相机跟随

      获得相机位置

        public Transform Camera;

      获得相机下一次的相对位置

    public Vector3 _camerRelativePosition; 

      获得相机移动的距离

    _camerRelativePosition = Camera.position - transform.position;

      使用DG.Tweening插件,添加移动相机的动画

        void MoveCamera()
        {
            Camera.DOMove(transform.position + _camerRelativePosition,1);
        }

    using DG.Tweening;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Player : MonoBehaviour {
    
        public float Factoer=1;
    
        public float MaxDistance = 5;
        public GameObject Stage;
    
        //获取相机位置
        public Transform Camera;
    
        private Rigidbody _rigidbody;
        private float _stateTime;
        //当前盒子物体
        private GameObject _currentStage;
        private Collider _lastCollisionCollider;
    
        //相机的相对位置
        public Vector3 _camerRelativePosition; 
    
        // Use this for initialization
        void Start () {
            _rigidbody = GetComponent<Rigidbody>();
            //修改物理组件的重心到body的底部
            _rigidbody.centerOfMass = Vector3.zero;
    
            _currentStage = Stage;
            _lastCollisionCollider = _currentStage.GetComponent<Collider>();
            SpawnStage();
    
            _camerRelativePosition = Camera.position - transform.position;
        }
        
        // Update is called once per frame
        void Update () {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                _stateTime = Time.time;
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                var elapse = Time.time - _stateTime;
                OnJump(elapse);
            }
        }
    
        void OnJump(float elapse)
        {
            _rigidbody.AddForce(new Vector3(1,1,0)*elapse* Factoer,ForceMode.Impulse);
        }
    
        void SpawnStage()
        {
            var stage = Instantiate(Stage);
            stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),0,0);
        }
    
        //如果有别的物体和本物体发生变化,会触发这函数
        void OnCollisionEnter(Collision collision)
        {
           if(collision.gameObject.name.Contains("Stage") && collision.collider!=_lastCollisionCollider)
            {
                _lastCollisionCollider = collision.collider;
                _currentStage = collision.gameObject;
                SpawnStage();
                MoveCamera();
            }
        }
    
        void MoveCamera()
        {
            Camera.DOMove(transform.position + _camerRelativePosition,1);
        }
    }
    View Code

    死亡判定及重新开始

      小人落到地面上就可以判断游戏结束了 

      当Player碰到地面时,重新加载场景

           if(collision.gameObject.name=="Ground")
            {
                //本局游戏结束,重新开始
                SceneManager.LoadScene("Gary");
            }

    (要注意光源问题,光源保存在缓存中,重新加载场景时不会加载光照)

        

      重新开始游戏效果

    using DG.Tweening;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class Player : MonoBehaviour {
    
        public float Factoer=1;
    
        public float MaxDistance = 5;
        public GameObject Stage;
    
        //获取相机位置
        public Transform Camera;
    
        private Rigidbody _rigidbody;
        private float _stateTime;
        //当前盒子物体
        private GameObject _currentStage;
        private Collider _lastCollisionCollider;
    
        //相机的相对位置
        public Vector3 _camerRelativePosition; 
    
        // Use this for initialization
        void Start () {
            _rigidbody = GetComponent<Rigidbody>();
            //修改物理组件的重心到body的底部
            _rigidbody.centerOfMass = Vector3.zero;
    
            _currentStage = Stage;
            _lastCollisionCollider = _currentStage.GetComponent<Collider>();
            SpawnStage();
    
            _camerRelativePosition = Camera.position - transform.position;
        }
        
        // Update is called once per frame
        void Update () {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                _stateTime = Time.time;
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                var elapse = Time.time - _stateTime;
                OnJump(elapse);
            }
        }
    
        void OnJump(float elapse)
        {
            _rigidbody.AddForce(new Vector3(1,1,0)*elapse* Factoer,ForceMode.Impulse);
        }
    
        void SpawnStage()
        {
            var stage = Instantiate(Stage);
            stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),0,0);
        }
    
        //如果有别的物体和本物体发生变化,会触发这函数
        void OnCollisionEnter(Collision collision)
        {
           if(collision.gameObject.name.Contains("Stage") && collision.collider!=_lastCollisionCollider)
            {
                _lastCollisionCollider = collision.collider;
                _currentStage = collision.gameObject;
                SpawnStage();
                MoveCamera();
            }
    
           if(collision.gameObject.name=="Ground")
            {
                //本局游戏结束,重新开始
                SceneManager.LoadScene("Gary");
            }
        }
    
        void MoveCamera()
        {
            Camera.DOMove(transform.position + _camerRelativePosition,1);
        }
    }
    Player.cs

    分数UI显示

      创建一个Text文本控件,并设置2D文本位置,右上角设置文字位置

      public Text ScoreText;
      private int _score;

      当跳到新的方块上时

           if(collision.gameObject.name.Contains("Stage") && collision.collider!=_lastCollisionCollider)
            {
                _lastCollisionCollider = collision.collider;
                _currentStage = collision.gameObject;
                SpawnStage();
                MoveCamera();
    
                _score++;
                ScoreText.text = _score.ToString();
            }

    角色蓄力粒子效果

      在Player上创建粒子系统Particle System

      将粒子系统Shape中的Shaper改为Hemisphere圆形发射,并修改Potation上的X值为-90并修改粒子系统的初始值

     修改脚本,当角色蓄力的时候才会显现出粒子效果

      public GameObject Particle;
    
            Particle = GameObject.Find("yellow");
            Particle.SetActive(false);
    
          if (Input.GetKeyDown(KeyCode.Space))
            {
                _stateTime = Time.time;
                Particle.SetActive(true);
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                var elapse = Time.time - _stateTime;
                OnJump(elapse);
                Particle.SetActive(false);
            }

    using DG.Tweening;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    using UnityEngine.UI;
    
    public class Player : MonoBehaviour {
    
        public float Factoer=1;
    
        public float MaxDistance = 4;
        public GameObject Stage;
    
        //获取相机位置
        public Transform Camera;
    
        private Rigidbody _rigidbody;
        private float _stateTime;
    
        public GameObject Particle;
    
        //当前盒子物体
        private GameObject _currentStage;
        private Collider _lastCollisionCollider;
    
        //相机的相对位置
        public Vector3 _camerRelativePosition;
    
        public Text ScoreText;
        private int _score;
    
        // Use this for initialization
        void Start () {
            _rigidbody = GetComponent<Rigidbody>();
            Particle = GameObject.Find("yellow");
            Particle.SetActive(false);
            //修改物理组件的重心到body的底部
            _rigidbody.centerOfMass = Vector3.zero;
    
            _currentStage = Stage;
            _lastCollisionCollider = _currentStage.GetComponent<Collider>();
            SpawnStage();
    
            _camerRelativePosition = Camera.position - transform.position;
            
        }
        
        // Update is called once per frame
        void Update () {
           
            if (Input.GetKeyDown(KeyCode.Space))
            {
                _stateTime = Time.time;
                Particle.SetActive(true);
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                var elapse = Time.time - _stateTime;
                OnJump(elapse);
                Particle.SetActive(false);
            }
        }
    
        void OnJump(float elapse)
        {
            _rigidbody.AddForce(new Vector3(1,1,0)*elapse* Factoer,ForceMode.Impulse);
        }
    
        void SpawnStage()
        {
            var stage = Instantiate(Stage);
            stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),0,0);
        }
    
        //如果有别的物体和本物体发生变化,会触发这函数
        void OnCollisionEnter(Collision collision)
        {
           if(collision.gameObject.name.Contains("Stage") && collision.collider!=_lastCollisionCollider)
            {
                _lastCollisionCollider = collision.collider;
                _currentStage = collision.gameObject;
                SpawnStage();
                MoveCamera();
    
                _score++;
                ScoreText.text = _score.ToString();
            }
    
           if(collision.gameObject.name=="Ground")
            {
                //本局游戏结束,重新开始
                SceneManager.LoadScene("Gary");
            }
        }
    
        void MoveCamera()
        {
             Camera.DOMove(transform.position + _camerRelativePosition,1);
        }
    }
    Player.cs

    角色蓄力动画

      当按下空格键时,小人物的身体进行缩放,我们可以通过脚本来设置

        public GameObject Particle;
    
        Particle = GameObject.Find("yellow");
        Particle.SetActive(false);
        void Update () {
           
            if (Input.GetKeyDown(KeyCode.Space))
            {
                _stateTime = Time.time;
                Particle.SetActive(true);
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                var elapse = Time.time - _stateTime;
                OnJump(elapse);
                Particle.SetActive(false);
    
                Body.transform.DOScale(0.1f,1);
                Head.transform.DOLocalMoveY(0.29f,0.5f);
            }
            if (Input.GetKey(KeyCode.Space))
            {
                Body.transform.localScale += new Vector3(1, -1, 1) * 0.05f * Time.deltaTime;
                Head.transform.localPosition += new Vector3(0,-1,0)*0.1f*Time.deltaTime;
            }
    
        }

      

    using DG.Tweening;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    using UnityEngine.UI;
    
    public class Player : MonoBehaviour {
    
        public float Factoer=1;
    
        public float MaxDistance = 4;
        public GameObject Stage;
    
        //获取相机位置
        public Transform Camera;
    
        private Rigidbody _rigidbody;
        private float _stateTime;
    
        public GameObject Particle;
    
        public Transform Head;
        public Transform Body;
    
        //当前盒子物体
        private GameObject _currentStage;
        private Collider _lastCollisionCollider;
    
        //相机的相对位置
        public Vector3 _camerRelativePosition;
    
        public Text ScoreText;
        private int _score;
    
        // Use this for initialization
        void Start () {
            _rigidbody = GetComponent<Rigidbody>();
            Particle = GameObject.Find("yellow");
            Particle.SetActive(false);
            //修改物理组件的重心到body的底部
            _rigidbody.centerOfMass = Vector3.zero;
    
            _currentStage = Stage;
            _lastCollisionCollider = _currentStage.GetComponent<Collider>();
            SpawnStage();
    
            _camerRelativePosition = Camera.position - transform.position;
            
        }
        
        // Update is called once per frame
        void Update () {
           
            if (Input.GetKeyDown(KeyCode.Space))
            {
                _stateTime = Time.time;
                Particle.SetActive(true);
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                var elapse = Time.time - _stateTime;
                OnJump(elapse);
                Particle.SetActive(false);
    
                Body.transform.DOScale(0.1f,1);
                Head.transform.DOLocalMoveY(0.29f,0.5f);
            }
            if (Input.GetKey(KeyCode.Space))
            {
                Body.transform.localScale += new Vector3(1, -1, 1) * 0.05f * Time.deltaTime;
                Head.transform.localPosition += new Vector3(0,-1,0)*0.1f*Time.deltaTime;
            }
    
        }
    
        void OnJump(float elapse)
        {
            _rigidbody.AddForce(new Vector3(1,1,0)*elapse* Factoer,ForceMode.Impulse);
        }
    
        void SpawnStage()
        {
            var stage = Instantiate(Stage);
            stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),0,0);
        }
    
        //如果有别的物体和本物体发生变化,会触发这函数
        void OnCollisionEnter(Collision collision)
        {
           if(collision.gameObject.name.Contains("Stage") && collision.collider!=_lastCollisionCollider)
            {
                _lastCollisionCollider = collision.collider;
                _currentStage = collision.gameObject;
                SpawnStage();
                MoveCamera();
    
                _score++;
                ScoreText.text = _score.ToString();
            }
    
           if(collision.gameObject.name=="Ground")
            {
                //本局游戏结束,重新开始
                SceneManager.LoadScene("Gary");
            }
        }
    
        void MoveCamera()
        {
             Camera.DOMove(transform.position + _camerRelativePosition,1);
        }
    }
    Player.cs

    盒子蓄力动画

      盒子缩放沿着轴心缩放

     _currentStage.transform.localScale += new Vector3(0, -1, 0)*0.15f*Time.deltaTime;
                _currentStage.transform.localPosition += new Vector3(0, -1, 0) * 0.15f * Time.deltaTime;

      盒子恢复形状

    _currentStage.transform.DOLocalMoveY(0.25f,0.2f);
                _currentStage.transform.DOScale(new Vector3(1,0.5f,1),0.2f);

    using DG.Tweening;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    using UnityEngine.UI;
    
    public class Player : MonoBehaviour {
    
        public float Factoer=1;
    
        public float MaxDistance = 4;
        public GameObject Stage;
    
        //获取相机位置
        public Transform Camera;
    
        private Rigidbody _rigidbody;
        private float _stateTime;
    
        public GameObject Particle;
    
        public Transform Head;
        public Transform Body;
    
        //当前盒子物体
        private GameObject _currentStage;
        private Collider _lastCollisionCollider;
    
        //相机的相对位置
        public Vector3 _camerRelativePosition;
    
        public Text ScoreText;
        private int _score;
    
        // Use this for initialization
        void Start () {
            _rigidbody = GetComponent<Rigidbody>();
            Particle = GameObject.Find("yellow");
            Particle.SetActive(false);
            //修改物理组件的重心到body的底部
            _rigidbody.centerOfMass = Vector3.zero;
    
            _currentStage = Stage;
            _lastCollisionCollider = _currentStage.GetComponent<Collider>();
            SpawnStage();
    
            _camerRelativePosition = Camera.position - transform.position;
            
        }
        
        // Update is called once per frame
        void Update () {
           
            if (Input.GetKeyDown(KeyCode.Space))
            {
                _stateTime = Time.time;
                Particle.SetActive(true);
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                var elapse = Time.time - _stateTime;
                OnJump(elapse);
                Particle.SetActive(false);
    
                Body.transform.DOScale(0.1f,1);
                Head.transform.DOLocalMoveY(0.29f,0.5f);
    
                _currentStage.transform.DOLocalMoveY(0.25f,0.2f);
                _currentStage.transform.DOScale(new Vector3(1,0.5f,1),0.2f);
            }
            if (Input.GetKey(KeyCode.Space))
            {
                Body.transform.localScale += new Vector3(1, -1, 1) * 0.05f * Time.deltaTime;
                Head.transform.localPosition += new Vector3(0,-1,0)*0.1f*Time.deltaTime;
    
                //盒子缩放沿着轴心缩放
                _currentStage.transform.localScale += new Vector3(0, -1, 0)*0.15f*Time.deltaTime;
                _currentStage.transform.localPosition += new Vector3(0, -1, 0) * 0.15f * Time.deltaTime;
            }
    
        }
    
        void OnJump(float elapse)
        {
            _rigidbody.AddForce(new Vector3(1,1,0)*elapse* Factoer,ForceMode.Impulse);
        }
    
        void SpawnStage()
        {
            var stage = Instantiate(Stage);
            stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),0,0);
        }
    
        //如果有别的物体和本物体发生变化,会触发这函数
        void OnCollisionEnter(Collision collision)
        {
           if(collision.gameObject.name.Contains("Stage") && collision.collider!=_lastCollisionCollider)
            {
                _lastCollisionCollider = collision.collider;
                _currentStage = collision.gameObject;
                SpawnStage();
                MoveCamera();
    
                _score++;
                ScoreText.text = _score.ToString();
            }
    
           if(collision.gameObject.name=="Ground")
            {
                //本局游戏结束,重新开始
                SceneManager.LoadScene("Gary");
            }
        }
    
        void MoveCamera()
        {
             Camera.DOMove(transform.position + _camerRelativePosition,1);
        }
    }
    Player.cs

    盒子随机大小及颜色

      

      随机设置盒子的大小

            var randomScale = Random.Range(0.5f,1);
            Stage.transform.localScale = new Vector3(randomScale, 0.5f, randomScale);

      随机设置盒子的颜色

    stage.GetComponent<Renderer>().material.color = new Color(Random.Range(0.1f, 1), Random.Range(0.1f, 1), Random.Range(0.1f, 1));

    using DG.Tweening;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    using UnityEngine.UI;
    
    public class Player : MonoBehaviour {
    
        public float Factoer=1;
    
        public float MaxDistance = 4;
        public GameObject Stage;
    
        //获取相机位置
        public Transform Camera;
    
        private Rigidbody _rigidbody;
        private float _stateTime;
    
        public GameObject Particle;
    
        public Transform Head;
        public Transform Body;
    
        //当前盒子物体
        private GameObject _currentStage;
        private Collider _lastCollisionCollider;
    
        //相机的相对位置
        public Vector3 _camerRelativePosition;
    
        public Text ScoreText;
        private int _score;
    
        // Use this for initialization
        void Start () {
            _rigidbody = GetComponent<Rigidbody>();
            Particle = GameObject.Find("yellow");
            Particle.SetActive(false);
            //修改物理组件的重心到body的底部
            _rigidbody.centerOfMass = Vector3.zero;
    
            _currentStage = Stage;
            _lastCollisionCollider = _currentStage.GetComponent<Collider>();
            SpawnStage();
    
            _camerRelativePosition = Camera.position - transform.position;
            
        }
        
        // Update is called once per frame
        void Update () {
           
            if (Input.GetKeyDown(KeyCode.Space))
            {
                _stateTime = Time.time;
                Particle.SetActive(true);
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                var elapse = Time.time - _stateTime;
                OnJump(elapse);
                Particle.SetActive(false);
    
                Body.transform.DOScale(0.1f,1);
                Head.transform.DOLocalMoveY(0.29f,0.5f);
    
                _currentStage.transform.DOLocalMoveY(0.25f,0.2f);
                _currentStage.transform.DOScale(new Vector3(1,0.5f,1),0.2f);
            }
            if (Input.GetKey(KeyCode.Space))
            {
                Body.transform.localScale += new Vector3(1, -1, 1) * 0.05f * Time.deltaTime;
                Head.transform.localPosition += new Vector3(0,-1,0)*0.1f*Time.deltaTime;
    
                //盒子缩放沿着轴心缩放
                _currentStage.transform.localScale += new Vector3(0, -1, 0)*0.15f*Time.deltaTime;
                _currentStage.transform.localPosition += new Vector3(0, -1, 0) * 0.15f * Time.deltaTime;
            }
    
        }
    
        void OnJump(float elapse)
        {
            _rigidbody.AddForce(new Vector3(1,1,0)*elapse* Factoer,ForceMode.Impulse);
        }
    
        void SpawnStage()
        {
            var stage = Instantiate(Stage);
            stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),0,0);
    
    
            var randomScale = Random.Range(0.5f,1);
            Stage.transform.localScale = new Vector3(randomScale, 0.5f, randomScale);
    
            stage.GetComponent<Renderer>().material.color = new Color(Random.Range(0.1f, 1), Random.Range(0.1f, 1), Random.Range(0.1f, 1));
    
        }
    
        //如果有别的物体和本物体发生变化,会触发这函数
        void OnCollisionEnter(Collision collision)
        {
           if(collision.gameObject.name.Contains("Stage") && collision.collider!=_lastCollisionCollider)
            {
                _lastCollisionCollider = collision.collider;
                _currentStage = collision.gameObject;
                SpawnStage();
                MoveCamera();
    
                _score++;
                ScoreText.text = _score.ToString();
            }
    
           if(collision.gameObject.name=="Ground")
            {
                //本局游戏结束,重新开始
                SceneManager.LoadScene("Gary");
            }
        }
    
        void MoveCamera()
        {
             Camera.DOMove(transform.position + _camerRelativePosition,1);
        }
    }
    Player.cs

    盒子随机方向生成

      初始的时候设置生成的方向是沿X轴正方向

        Vector3 _direction = new Vector3(1, 0, 0);

      随机生成跳台

        void RandomDirection()
        {
            var seed = Random.Range(0,2);
            if(seed == 0)
            {
                _direction = new Vector3(1, 0, 0);
            }
            else
            {
                _direction = new Vector3(0,0,1);
            }
        }
       void MoveCamera()
        {
             Camera.DOMove(transform.position + _camerRelativePosition,1);
        }

      改变小人物跳跃方向

        void OnJump(float elapse)
        {
            _rigidbody.AddForce((new Vector3(0,1,0)+_direction)*elapse* Factoer,ForceMode.Impulse);
        }

    using DG.Tweening;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    using UnityEngine.UI;
    
    public class Player : MonoBehaviour {
    
        public float Factoer=1;
    
        public float MaxDistance = 4;
        public GameObject Stage;
    
        //获取相机位置
        public Transform Camera;
    
        private Rigidbody _rigidbody;
        private float _stateTime;
    
        public GameObject Particle;
    
        public Transform Head;
        public Transform Body;
    
        //当前盒子物体
        private GameObject _currentStage;
        private Collider _lastCollisionCollider;
    
        //相机的相对位置
        public Vector3 _camerRelativePosition;
    
        public Text ScoreText;
        private int _score;
    
        Vector3 _direction = new Vector3(1, 0, 0);
    
        // Use this for initialization
        void Start () {
            _rigidbody = GetComponent<Rigidbody>();
            Particle = GameObject.Find("yellow");
            Particle.SetActive(false);
            //修改物理组件的重心到body的底部
            _rigidbody.centerOfMass = Vector3.zero;
    
            _currentStage = Stage;
            _lastCollisionCollider = _currentStage.GetComponent<Collider>();
            SpawnStage();
    
            _camerRelativePosition = Camera.position - transform.position;
            
        }
        
        // Update is called once per frame
        void Update () {
           
            if (Input.GetKeyDown(KeyCode.Space))
            {
                _stateTime = Time.time;
                Particle.SetActive(true);
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                var elapse = Time.time - _stateTime;
                OnJump(elapse);
                Particle.SetActive(false);
    
                Body.transform.DOScale(0.1f,1);
                Head.transform.DOLocalMoveY(0.29f,0.5f);
    
                _currentStage.transform.DOLocalMoveY(0.25f,0.2f);
                _currentStage.transform.DOScale(new Vector3(1,0.5f,1),0.2f);
            }
            if (Input.GetKey(KeyCode.Space))
            {
                Body.transform.localScale += new Vector3(1, -1, 1) * 0.05f * Time.deltaTime;
                Head.transform.localPosition += new Vector3(0,-1,0)*0.1f*Time.deltaTime;
    
                //盒子缩放沿着轴心缩放
                _currentStage.transform.localScale += new Vector3(0, -1, 0)*0.15f*Time.deltaTime;
                _currentStage.transform.localPosition += new Vector3(0, -1, 0) * 0.15f * Time.deltaTime;
            }
    
        }
    
        void OnJump(float elapse)
        {
            _rigidbody.AddForce((new Vector3(0,1,0)+_direction)*elapse* Factoer,ForceMode.Impulse);
        }
    
        void SpawnStage()
        {
            var stage = Instantiate(Stage);
            stage.transform.position = _currentStage.transform.position + _direction * Random.Range(1.1f,MaxDistance) ;
    
    
            var randomScale = Random.Range(0.5f,1);
            Stage.transform.localScale = new Vector3(randomScale, 0.5f, randomScale);
    
            stage.GetComponent<Renderer>().material.color = new Color(Random.Range(0.01f, 1), Random.Range(0.01f, 1), Random.Range(0.01f, 1));
    
        }
    
        //如果有别的物体和本物体发生变化,会触发这函数
        void OnCollisionEnter(Collision collision)
        {
           if(collision.gameObject.name.Contains("Stage") && collision.collider!=_lastCollisionCollider)
            {
                _lastCollisionCollider = collision.collider;
                _currentStage = collision.gameObject;
                RandomDirection();
                SpawnStage();
                MoveCamera();
    
                _score++;
                ScoreText.text = _score.ToString();
            }
    
           if(collision.gameObject.name=="Ground")
            {
                //本局游戏结束,重新开始
                SceneManager.LoadScene("Gary");
            }
        }
    
        void RandomDirection()
        {
            var seed = Random.Range(0,2);
            if(seed == 0)
            {
                _direction = new Vector3(1, 0, 0);
            }
            else
            {
                _direction = new Vector3(0,0,1);
            }
            
        }
    
        void MoveCamera()
        {
             Camera.DOMove(transform.position + _camerRelativePosition,1);
        }
    }
    Player.cs

      

    ========================萌萌的分割线(ノ`Д)ノ============================

    想加个游戏联网排行榜:  

      LeanCloud官网  传送门

      下载LeanCloud-Unity-SDK-20180808.1.zip  传送门

     

    using DG.Tweening;
    using LeanCloud;
    using System.Collections;
    using System.Collections.Generic;
    using UniRx;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    using UnityEngine.UI;
    
    public class Player : MonoBehaviour {
    
        public float Factoer=1;
    
        public float MaxDistance = 4;
        public GameObject Stage;
     
        //获取相机的位置
          public Transform Camera;
    
         private Rigidbody _rigidbody;
        private float _stateTime;
    
        public GameObject Particle;
    
        public Transform Head;
        public Transform Body;
    
        //当前盒子物体
        private GameObject _currentStage;
        private Collider _lastCollisionCollider;
    
         public Button RestarButton;
    
        //相机的相对位置
         public Vector3  _camerRelativePosition;
    
        public Text ScoreText;
        private int _score;
    
        public GameObject SaveScorePanel;
        public InputField NameFiled;
        public Button SaveButton;
    
        public GameObject RankPanel;
         public GameObject RankItem;
    
          Vector3 _direction = new Vector3(1, 0, 0);
    
        // Use this for initialization
         void Start () {
    
    
             _rigidbody = GetComponent<Rigidbody>();
             Particle = GameObject.Find("yellow");
            Particle.SetActive(false);
            
             _rigidbody.centerOfMass = Vector3.zero ;
    
            _currentStage = Stage;
            _lastCollisionCollider = _currentStage.GetComponent<Collider>();
            SpawnStage();
    
             _camerRelativePosition = Camera.position - transform.position;
            
            SaveButton.onClick.AddListener(OnClickSaveButton);
            RestarButton.onClick.AddListener(()=> {
                SceneManager.LoadScene(0);
            });
            MainThreadDispatcher.Initialize();
        }
        
        // Update is called once per frame
        void Update () {
           
            if (Input.GetKeyDown(KeyCode.Space))
            {
                _stateTime = Time.time;
                Particle.SetActive(true);
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                var elapse = Time.time - _stateTime;
                OnJump(elapse);
                Particle.SetActive(false);
    
                Body.transform.DOScale(0.1f,1);
                Head.transform.DOLocalMoveY(0.29f,0.5f);
    
                _currentStage.transform.DOLocalMoveY(0.25f,0.2f);
                _currentStage.transform.DOScale(new Vector3(1,0.5f,1),0.2f);
            }
            if (Input.GetKey(KeyCode.Space))
            {
                Body.transform.localScale += new Vector3(1, -1, 1) * 0.05f * Time.deltaTime;
                Head.transform.localPosition += new Vector3(0,-1,0)*0.1f*Time.deltaTime;
    
                //盒子缩放沿着轴心缩放
                _currentStage.transform.localScale += new Vector3(0, -1, 0)*0.15f*Time.deltaTime;
                _currentStage.transform.localPosition += new Vector3(0, -1, 0) * 0.15f * Time.deltaTime;
            }
    
        }
    
    
    
        void OnJump(float elapse)
        {
            _rigidbody.AddForce((new Vector3(0,1,0)+_direction)*elapse* Factoer,ForceMode.Impulse);
        }
    
        void SpawnStage()
        {
            var stage = Instantiate(Stage);
            stage.transform.position = _currentStage.transform.position + _direction * Random.Range(1.1f,MaxDistance) ;
    
    
            var randomScale = Random.Range(0.5f,1);
            Stage.transform.localScale = new Vector3(randomScale, 0.5f, randomScale);
    
            stage.GetComponent<Renderer>().material.color = new Color(Random.Range(0.01f, 1), Random.Range(0.01f, 1), Random.Range(0.01f, 1));
    
        }
    
        //如果有别的物体和本物体发生变化,会触发这函数
        void OnCollisionEnter(Collision collision)
        {
           if(collision.gameObject.name.Contains("Stage") && collision.collider!=_lastCollisionCollider)
            {
                _lastCollisionCollider = collision.collider;
                _currentStage = collision.gameObject;
                RandomDirection();
                SpawnStage();
                 MoveCamera();
    
                _score++;
                 ScoreText.text = _score.ToString();
            }
    
           if(collision.gameObject.name=="Ground")
            {
                 //本局游戏结束,重新开始
                //SceneManager.LoadScene("Gary");
                //游戏结束,显示上传分数panel
                SaveScorePanel.SetActive(true);
                 RankPanel.SetActive(true);
            }
        }
    
        void RandomDirection()
        {
            var seed = Random.Range(0,2);
            if(seed == 0)
            {
                _direction = new Vector3(1, 0, 0);
            }
            else
            {
                _direction = new Vector3(0,0,1);
            }
            
        }
    
        void MoveCamera()
        {
             Camera.DOMove(transform.position + _camerRelativePosition,1);
        }
    
        void OnClickSaveButton()
        {
            var nickname = NameFiled.text;
    
            AVObject gameScore = new AVObject("GameScore");
            gameScore["score"] = _score;
            gameScore["playerName"] = nickname;
            gameScore.SaveAsync().ContinueWith(_=> {
    
                showRankPanel();
            });
            SaveScorePanel.SetActive(true);
            
        }
    
        void showRankPanel()
        {
            AVQuery<AVObject> query = new AVQuery<AVObject>("GameScore").OrderByDescending("score").Limit(10);
            query.FindAsync().ContinueWith(t=>
            {
                var results = t.Result;
                var scores = new List<string>();
    
                foreach(var result in results)
                {
                    var score = result["playerName"]+"  :   "+result["score"];
                    scores.Add(score);
                }
    
                 MainThreadDispatcher.Send(_ =>
                {
                    foreach (var score in scores)
                    {
                        var item = Instantiate(RankItem);
                        item.SetActive(true);
                        item.GetComponent<Text>().text = score;
                        item.transform.SetParent(RankItem.transform.parent);
                    }
                    RankPanel.SetActive(true);
    
                },null);
            });
        }
    }
    Player.cs

     工程游戏中测试成功~

      可导出时运行却失败了!!

    (如需转载学习,请标明出处)
  • 相关阅读:
    插入排序
    JavaMail学习笔记
    汉诺塔问题
    使用Three.js绘制一个虚拟城市
    jquery flotcharts使用简介
    用CSS hack技术解决浏览器兼容性问题.
    IE条件注释详解.
    让IE6也认识!important
    代码重构(转)
    模版+数据分离渲染方式的设计与实现
  • 原文地址:https://www.cnblogs.com/1138720556Gary/p/9488495.html
Copyright © 2011-2022 走看看