Unity3D_(遊戲)跳一跳超簡單製做過程

 

 

  

跳一跳git

 

  工程文件界面github

 

  遊戲界面緩存

  

 

  腳本app

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可執行文件):傳送門dom

    小遊戲沒有用到素材、資源包(DOTween插件不算)ide

  一個腳本  函數

 

實現過程測試

 

  【腳本中public外部引用的控件須要本身拖拽到相應位置上!!!】動畫

 

建立一個3D場景this

 

 

  建立一個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();
        }
    }

 

  一、輕輕一跳在同一個盒子上時,不能在從新生成新的盒子

  二、沒有跳到下一個盒子上,不能再生成新的盒子

 

 

 

相機跟隨

 

  得到相機位置

    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

 

 工程遊戲中測試成功~

 

 

  可導出時運行卻失敗了!!

 

 

 

相關文章
相關標籤/搜索