跳一跳

  工程文件界面

  游戏界面

  

  脚本

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=; public float MaxDistance = ;
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(, , ); // 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,);
Head.transform.DOLocalMoveY(0.29f,0.5f); _currentStage.transform.DOLocalMoveY(0.25f,0.2f);
_currentStage.transform.DOScale(new Vector3(,0.5f,),0.2f);
}
if (Input.GetKey(KeyCode.Space))
{
Body.transform.localScale += new Vector3(, -, ) * 0.05f * Time.deltaTime;
Head.transform.localPosition += new Vector3(,-,)*0.1f*Time.deltaTime; //盒子缩放沿着轴心缩放
_currentStage.transform.localScale += new Vector3(, -, )*0.15f*Time.deltaTime;
_currentStage.transform.localPosition += new Vector3(, -, ) * 0.15f * Time.deltaTime;
} } void OnJump(float elapse)
{
_rigidbody.AddForce((new Vector3(,,)+_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,);
Stage.transform.localScale = new Vector3(randomScale, 0.5f, randomScale); stage.GetComponent<Renderer>().material.color = new Color(Random.Range(0.01f, ), Random.Range(0.01f, ), Random.Range(0.01f, )); } //如果有别的物体和本物体发生变化,会触发这函数
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(,);
if(seed == )
{
_direction = new Vector3(, , );
}
else
{
_direction = new Vector3(,,);
} } void MoveCamera()
{
Camera.DOMove(transform.position + _camerRelativePosition,);
}
} 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=;

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Player : MonoBehaviour { public float Factoer=; 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(,,)*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),,);
    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),,);
}

  

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Player : MonoBehaviour { public float Factoer=; public float MaxDistance = ;
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(,,)*elapse* Factoer,ForceMode.Impulse);
} void SpawnStage()
{
var stage = Instantiate(Stage);
stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),,);
}
}

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,);
}

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Player : MonoBehaviour { public float Factoer=; public float MaxDistance = ;
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(,,)*elapse* Factoer,ForceMode.Impulse);
} void SpawnStage()
{
var stage = Instantiate(Stage);
stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),,);
} //如果有别的物体和本物体发生变化,会触发这函数
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,);
}
}

死亡判定及重新开始

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

  当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=; public float MaxDistance = ;
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(,,)*elapse* Factoer,ForceMode.Impulse);
} void SpawnStage()
{
var stage = Instantiate(Stage);
stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),,);
} //如果有别的物体和本物体发生变化,会触发这函数
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,);
}
}

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=; public float MaxDistance = ;
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(,,)*elapse* Factoer,ForceMode.Impulse);
} void SpawnStage()
{
var stage = Instantiate(Stage);
stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),,);
} //如果有别的物体和本物体发生变化,会触发这函数
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,);
}
}

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,);
Head.transform.DOLocalMoveY(0.29f,0.5f);
}
if (Input.GetKey(KeyCode.Space))
{
Body.transform.localScale += new Vector3(, -, ) * 0.05f * Time.deltaTime;
Head.transform.localPosition += new Vector3(,-,)*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=; public float MaxDistance = ;
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,);
Head.transform.DOLocalMoveY(0.29f,0.5f);
}
if (Input.GetKey(KeyCode.Space))
{
Body.transform.localScale += new Vector3(, -, ) * 0.05f * Time.deltaTime;
Head.transform.localPosition += new Vector3(,-,)*0.1f*Time.deltaTime;
} } void OnJump(float elapse)
{
_rigidbody.AddForce(new Vector3(,,)*elapse* Factoer,ForceMode.Impulse);
} void SpawnStage()
{
var stage = Instantiate(Stage);
stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),,);
} //如果有别的物体和本物体发生变化,会触发这函数
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,);
}
}

Player.cs

盒子蓄力动画

  盒子缩放沿着轴心缩放

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

  盒子恢复形状

_currentStage.transform.DOLocalMoveY(0.25f,0.2f);
_currentStage.transform.DOScale(new Vector3(,0.5f,),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=; public float MaxDistance = ;
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,);
Head.transform.DOLocalMoveY(0.29f,0.5f); _currentStage.transform.DOLocalMoveY(0.25f,0.2f);
_currentStage.transform.DOScale(new Vector3(,0.5f,),0.2f);
}
if (Input.GetKey(KeyCode.Space))
{
Body.transform.localScale += new Vector3(, -, ) * 0.05f * Time.deltaTime;
Head.transform.localPosition += new Vector3(,-,)*0.1f*Time.deltaTime; //盒子缩放沿着轴心缩放
_currentStage.transform.localScale += new Vector3(, -, )*0.15f*Time.deltaTime;
_currentStage.transform.localPosition += new Vector3(, -, ) * 0.15f * Time.deltaTime;
} } void OnJump(float elapse)
{
_rigidbody.AddForce(new Vector3(,,)*elapse* Factoer,ForceMode.Impulse);
} void SpawnStage()
{
var stage = Instantiate(Stage);
stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),,);
} //如果有别的物体和本物体发生变化,会触发这函数
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,);
}
}

Player.cs

盒子随机大小及颜色

  

  随机设置盒子的大小

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

  随机设置盒子的颜色

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

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=; public float MaxDistance = ;
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,);
Head.transform.DOLocalMoveY(0.29f,0.5f); _currentStage.transform.DOLocalMoveY(0.25f,0.2f);
_currentStage.transform.DOScale(new Vector3(,0.5f,),0.2f);
}
if (Input.GetKey(KeyCode.Space))
{
Body.transform.localScale += new Vector3(, -, ) * 0.05f * Time.deltaTime;
Head.transform.localPosition += new Vector3(,-,)*0.1f*Time.deltaTime; //盒子缩放沿着轴心缩放
_currentStage.transform.localScale += new Vector3(, -, )*0.15f*Time.deltaTime;
_currentStage.transform.localPosition += new Vector3(, -, ) * 0.15f * Time.deltaTime;
} } void OnJump(float elapse)
{
_rigidbody.AddForce(new Vector3(,,)*elapse* Factoer,ForceMode.Impulse);
} void SpawnStage()
{
var stage = Instantiate(Stage);
stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),,); var randomScale = Random.Range(0.5f,);
Stage.transform.localScale = new Vector3(randomScale, 0.5f, randomScale); stage.GetComponent<Renderer>().material.color = new Color(Random.Range(0.1f, ), Random.Range(0.1f, ), Random.Range(0.1f, )); } //如果有别的物体和本物体发生变化,会触发这函数
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,);
}
}

Player.cs

盒子随机方向生成

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

    Vector3 _direction = new Vector3(, , );

  随机生成跳台

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

  改变小人物跳跃方向

    void OnJump(float elapse)
{
_rigidbody.AddForce((new Vector3(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=; public float MaxDistance = ;
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(, , ); // 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,);
Head.transform.DOLocalMoveY(0.29f,0.5f); _currentStage.transform.DOLocalMoveY(0.25f,0.2f);
_currentStage.transform.DOScale(new Vector3(,0.5f,),0.2f);
}
if (Input.GetKey(KeyCode.Space))
{
Body.transform.localScale += new Vector3(, -, ) * 0.05f * Time.deltaTime;
Head.transform.localPosition += new Vector3(,-,)*0.1f*Time.deltaTime; //盒子缩放沿着轴心缩放
_currentStage.transform.localScale += new Vector3(, -, )*0.15f*Time.deltaTime;
_currentStage.transform.localPosition += new Vector3(, -, ) * 0.15f * Time.deltaTime;
} } void OnJump(float elapse)
{
_rigidbody.AddForce((new Vector3(,,)+_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,);
Stage.transform.localScale = new Vector3(randomScale, 0.5f, randomScale); stage.GetComponent<Renderer>().material.color = new Color(Random.Range(0.01f, ), Random.Range(0.01f, ), Random.Range(0.01f, )); } //如果有别的物体和本物体发生变化,会触发这函数
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(,);
if(seed == )
{
_direction = new Vector3(, , );
}
else
{
_direction = new Vector3(,,);
} } void MoveCamera()
{
Camera.DOMove(transform.position + _camerRelativePosition,);
}
}

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=; public float MaxDistance = ;
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(, , ); // 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();
});
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,);
Head.transform.DOLocalMoveY(0.29f,0.5f); _currentStage.transform.DOLocalMoveY(0.25f,0.2f);
_currentStage.transform.DOScale(new Vector3(,0.5f,),0.2f);
}
if (Input.GetKey(KeyCode.Space))
{
Body.transform.localScale += new Vector3(, -, ) * 0.05f * Time.deltaTime;
Head.transform.localPosition += new Vector3(,-,)*0.1f*Time.deltaTime; //盒子缩放沿着轴心缩放
_currentStage.transform.localScale += new Vector3(, -, )*0.15f*Time.deltaTime;
_currentStage.transform.localPosition += new Vector3(, -, ) * 0.15f * Time.deltaTime;
} } void OnJump(float elapse)
{
_rigidbody.AddForce((new Vector3(,,)+_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,);
Stage.transform.localScale = new Vector3(randomScale, 0.5f, randomScale); stage.GetComponent<Renderer>().material.color = new Color(Random.Range(0.01f, ), Random.Range(0.01f, ), Random.Range(0.01f, )); } //如果有别的物体和本物体发生变化,会触发这函数
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(,);
if(seed == )
{
_direction = new Vector3(, , );
}
else
{
_direction = new Vector3(,,);
} } void MoveCamera()
{
Camera.DOMove(transform.position + _camerRelativePosition,);
} 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();
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

工程游戏中测试成功~

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

Unity3D_(游戏)跳一跳超简单制作过程的更多相关文章

  1. 用Kotlin破解Android版微信小游戏-跳一跳

    前言 微信又更新了,从更新日志上来看,似乎只是一次不痛不痒的小更新.不过,很快就有人发现,原来微信这次搞了个大动作——在小程序里加入了小游戏.今天也是朋友圈被刷爆的缘故. 看到网上 有人弄了一个破解版 ...

  2. 利用python实现微信小程序游戏跳一跳详细教程

    利用python实现微信小程序游戏跳一跳详细教程 1 先安装python 然后再安装pip <a href="http://newmiracle.cn/wp-content/uploa ...

  3. 微信小游戏跳一跳简单手动外挂(基于adb 和 python)

    只有两个python文件,代码很简单. shell.py: #coding:utf-8 import subprocess import math import os def execute_comm ...

  4. 微信小游戏“跳一跳”,Python“外挂”已上线

    微信又一次不声不响地搞了个大事情: “小游戏”上线了! 于是,在这辞旧迎新的时刻,毫无意外的又火了. 今天有多少人刷了,让我看到你们的双手! 喏,我已经尽力了…… 不过没关系,你们跳的再好,在毫无心理 ...

  5. WinSetupFromUSB - 超简单制作多合一系统安装启动U盘的工具 (支持Win/PE/Linux启动盘)

    很多同学都喜欢将电脑凌乱不堪的系统彻底重装以获得一个"全新的开始",但你会发现如今很多电脑都已经没有光驱了,因此制作一个U盘版的系统安装启动盘备用是非常必要的. 我们之前推荐过 I ...

  6. 用C#实现微信“跳一跳”小游戏的自动跳跃助手

    一.前言: 前段时间微信更新了新版本后,带来的一款H5小游戏“跳一跳”在各朋友圈里又火了起来,类似以前的“打飞机”游戏,这游戏玩法简单,但加上了积分排名功能后,却成了“装逼”的地方,于是很多人花钱花时 ...

  7. .NET开发一个微信跳一跳辅助程序

    昨天微信更新了,出现了一个小游戏"跳一跳",玩了一下 赶紧还蛮有意思的 但纯粹是拼手感的,玩了好久,终于搞了个135分拿了个第一名,没想到过一会就被朋友刷下去了,最高的也就200来 ...

  8. 从“跳一跳”来看微信小程序的未来

    从“跳一跳”来看微信小程序的未来   相信大家这两天都被微信新推出的小程序跳一跳刷爆了朋友圈,为了方便用户在使用过程中切换小程序,微信在这次6.6.1版本中加入了下拉可快速切换小程序的功能,而“跳一跳 ...

  9. 使用python玩跳一跳亲测使用步骤详解

    玩微信跳一跳,测测python跳一跳,顺便蹭一蹭热度: 参考博文 使用python玩跳一跳超详细使用教程 WIN10系统,安卓用户请直入此: python辅助作者github账号为:wangshub. ...

随机推荐

  1. MyISAM与InnoDB的索引差异

    数据库的索引分为主键索引(Primary Index)与普通索引(Secondary Index).InnoDB和MyISAM是怎么利用B+树来实现这两类索引的,又有什么差异呢?一.MyISAM的索引 ...

  2. Hive 教程(九)-python with hive

    本文介绍用 python 远程连接 hive,此时需要 hive 启动 hiveserver2 服务 windows 下报如下错误 thrift.transport.TTransport.TTrans ...

  3. 基于IdentityServer4的声明的授权

    ## 概述 基于Asp.net Core 1.1 ,使用IdentityServer4认证与授权. ## 参考资料 [微软教程](https://docs.microsoft.com/zh-cn/as ...

  4. 关于redis的几件小事(八)缓存与数据库双写时的数据一致性

    1.Cache aside pattern 这是最经典的 缓存+数据库 读写模式,操作如下: ①读的时候,先读缓存,缓存没有就读数据库,然后将取出的数据放到缓存,同时返回请求响应. ②更新的时候,先删 ...

  5. 为什么单线程的Redis却能支撑高并发

    Redis的高并发和快速原因 redis是基于内存的,内存的读写速度非常快: 核心是基于非阻塞的IO多路复用机制: redis是单线程的,反而省去了很多上下文切换线程的时间: 为什么Redis是单线程 ...

  6. angular实现三级联动

    (function(angular) { 'use strict'; var module = angular.module('timecube.shopManage.group.ctrls', [' ...

  7. jenkins自动部署代码到多台服务器

    下面讲一下如何通过一台jenkins服务器构建后将代码分发到其他的远程服务器,即jenkins自动部署代码到多台服务器. 1.下载 pulish over ssh 插件 2.系统管理 -> 系统 ...

  8. 使用SpringBoot做Javaweb时,数据交互遇到的问题

    有段时间没做过javaweb了,有点生疏了,js也忘记得差不多,所以今天下午做前后端交互的时候,传到后台的参数总是为空,前端控制台了报一个String parameter “xxx” is not p ...

  9. mongodb aggregate

    $project:修改输入文档的结构.可以用来重命名.增加或删除域,也可以用于创建计算结果以及嵌套文档. $match:用于过滤数据,只输出符合条件的文档.$match使用MongoDB的标准查询操作 ...

  10. centos swap分区

    swap分区         通常memory是机器的物理内存,读写速度低于cpu一个量级,但是高于磁盘不止一个量级.所以,程序和数据如果在内存的话,会有非常快的读写速度.但是,内存的造价是要高于磁盘 ...