跳一跳

  工程文件界面

  游戏界面

  

  脚本

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. CSP 俄罗斯方块(201604-2)

    问题描述 俄罗斯方块是俄罗斯人阿列克谢·帕基特诺夫发明的一款休闲游戏. 游戏在一个15行10列的方格图上进行,方格图上的每一个格子可能已经放置了方块,或者没有放置方块.每一轮,都会有一个新的由4个小方 ...

  2. 谷歌官方颜色库 MaterialDesignColor

    谷歌官方颜色库 MaterialDesignColor

  3. CSS3面包屑菜单导航

    在线演示 本地下载

  4. 剑指Offer 1-41 代码(python实现)

    今天主要写了一下offer 1-41题,余下的稍后整理 1 """ 1 镜像二叉树: 递归 """ def mirror(root): if ...

  5. cf 1163D Mysterious Code (字符串, dp)

    大意: 给定字符串$C$, 只含小写字母和'*', '*'表示可以替换为任意小写字母, 再给定字符串$S,T$, 求$S$在$C$中出现次数-$T$在$C$中出现次数最大值. 设$dp[i][j][k ...

  6. 一款完美代替微信小程序原生客服消息的工具:

    一.设置:无需开发,多种回复(自动+人工)   自动回复形式有3种: 打开客服消息(用户只要和客服互动过一次,再次点击进入,会收到设置好的自动回复) 关键词回复(用户在小程序中回复某个关键词内容时,会 ...

  7. springboot的一些注解

    springboot注解以及一些晦涩难理解的点介绍 @Validated 用于注入数值校验的注解(JSR303数据校验) @PropertySource 用于加载指定的配置文件,例如@Property ...

  8. scrapy-redis 实现分布式爬虫

    分布式爬虫 一 介绍 原来scrapy的Scheduler维护的是本机的任务队列(存放Request对象及其回调函数等信息)+本机的去重队列(存放访问过的url地址) 所以实现分布式爬取的关键就是,找 ...

  9. 使用Mybatis Generator自动生成代码

    MyBatis Generator(MBG)是MyBatis MyBatis 和iBATIS的代码生成器.它将为所有版本的MyBatis以及版本2.2.0之后的iBATIS版本生成代码.它将内省数据库 ...

  10. GeoJson格式与转换(shapefile)Geotools

    转自:https://blog.csdn.net/cobramonkey/article/details/71124888 作为大数据分析的重要工具,Hadoop在这一领域发挥着不可或缺的作用.有些人 ...