跳一跳

  工程文件界面

  游戏界面

  

  脚本

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. ASP.NET CORE CACHE的使用(含MemoryCache,Redis)

    原文:ASP.NET CORE CACHE的使用(含MemoryCache,Redis) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接 ...

  2. BASH的保护性编程技巧

    BASH的保护性编程技巧   shell常用逻辑判断 -b file 若文件存在且是一个块特殊文件,则为真 -c file 若文件存在且是一个字符特殊文件,则为真 -d file 若文件存在且是一个目 ...

  3. vmware虚拟机安装centos7.3

    vmware准备 CentOS准备,这里下载的是CentOS 7.3CentOS-7-x86_64-Everything-1611.iso 创建新的虚拟机 选择自定义安装 硬件兼容性默认最新的,不用动 ...

  4. Python time、datetime、os、random、sys、hashlib、json、shutil、logging、paramiko、subprocess、ConfigParser、xml、shelve模块的使用

    文章目录: 1. time & datetime模块 2. os模块 3. random模块 4. sys模块 5. hashlib模块 6. json模块 7. shutil模块 8. lo ...

  5. java8学习之收集器枚举特性深度解析与并行流原理

    首先先来找出上一次[http://www.cnblogs.com/webor2006/p/8353314.html]在最后举的那个并行流报错的问题,如下: 在来查找出上面异常的原因之前,当然得要一点点 ...

  6. ImportError: attempted relative import with no known parent package

    或者检查所导包是否存在__init__.py文件,没有则添加上即可使当前文件夹变为包.

  7. Java8使用lambda遍历List、Set、map

    public static void main(String[] args){ Map<String,String> map= new HashMap<>(); map.for ...

  8. Nginx动静分离经典案例配置

    随着Nginx高性能Web服务器大量被使用,目前Nginx最新稳定版为1.2.6,张宴兄在实际应用中大量使用Nginx,并分享Nginx高性能Web服务器知识,使得Nginx在国内也是飞速的发展.那今 ...

  9. http协议和i/o模型

    http协议----基于请求报文和响应报文完成一次http事务 应用层协议格式有两种: 文本(开发容易,但交互解析困难如http smtp),二进制(交互解析容易,但理解起来困难memocache) ...

  10. .net中[Serializable]序列化的应用

    原文链接:https://blog.csdn.net/wanlong360599336/article/details/9222459 浅析.NET中的Serialization 摘要 本文简要介绍了 ...