Unity3D_(游戏)跳一跳超简单制作过程
跳一跳
工程文件界面
游戏界面
脚本
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI; public class Player : MonoBehaviour { public float Factoer=; 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_(游戏)跳一跳超简单制作过程的更多相关文章
- 用Kotlin破解Android版微信小游戏-跳一跳
前言 微信又更新了,从更新日志上来看,似乎只是一次不痛不痒的小更新.不过,很快就有人发现,原来微信这次搞了个大动作——在小程序里加入了小游戏.今天也是朋友圈被刷爆的缘故. 看到网上 有人弄了一个破解版 ...
- 利用python实现微信小程序游戏跳一跳详细教程
利用python实现微信小程序游戏跳一跳详细教程 1 先安装python 然后再安装pip <a href="http://newmiracle.cn/wp-content/uploa ...
- 微信小游戏跳一跳简单手动外挂(基于adb 和 python)
只有两个python文件,代码很简单. shell.py: #coding:utf-8 import subprocess import math import os def execute_comm ...
- 微信小游戏“跳一跳”,Python“外挂”已上线
微信又一次不声不响地搞了个大事情: “小游戏”上线了! 于是,在这辞旧迎新的时刻,毫无意外的又火了. 今天有多少人刷了,让我看到你们的双手! 喏,我已经尽力了…… 不过没关系,你们跳的再好,在毫无心理 ...
- WinSetupFromUSB - 超简单制作多合一系统安装启动U盘的工具 (支持Win/PE/Linux启动盘)
很多同学都喜欢将电脑凌乱不堪的系统彻底重装以获得一个"全新的开始",但你会发现如今很多电脑都已经没有光驱了,因此制作一个U盘版的系统安装启动盘备用是非常必要的. 我们之前推荐过 I ...
- 用C#实现微信“跳一跳”小游戏的自动跳跃助手
一.前言: 前段时间微信更新了新版本后,带来的一款H5小游戏“跳一跳”在各朋友圈里又火了起来,类似以前的“打飞机”游戏,这游戏玩法简单,但加上了积分排名功能后,却成了“装逼”的地方,于是很多人花钱花时 ...
- .NET开发一个微信跳一跳辅助程序
昨天微信更新了,出现了一个小游戏"跳一跳",玩了一下 赶紧还蛮有意思的 但纯粹是拼手感的,玩了好久,终于搞了个135分拿了个第一名,没想到过一会就被朋友刷下去了,最高的也就200来 ...
- 从“跳一跳”来看微信小程序的未来
从“跳一跳”来看微信小程序的未来 相信大家这两天都被微信新推出的小程序跳一跳刷爆了朋友圈,为了方便用户在使用过程中切换小程序,微信在这次6.6.1版本中加入了下拉可快速切换小程序的功能,而“跳一跳 ...
- 使用python玩跳一跳亲测使用步骤详解
玩微信跳一跳,测测python跳一跳,顺便蹭一蹭热度: 参考博文 使用python玩跳一跳超详细使用教程 WIN10系统,安卓用户请直入此: python辅助作者github账号为:wangshub. ...
随机推荐
- 小白学习django第四站-关联数据库
使用mysql连接django首先要配置好相关环境 首先在setting.py配置数据库信息(需要现在mysql中创建一个数据库) 在setting.py那个目录的__init__.py文件中写入 之 ...
- expect批量分发密钥对
vim shell.exp #!/usr/bin/expect set timeout 10 set hostname [lindex $argv 0] set username [lindex $a ...
- Codeforces 1229B. Kamil and Making a Stream
传送门 注意到只要考虑祖先和后代之间的贡献 发现对于一个节点,他和所有祖先最多产生 $log$ 个不同的 $gcd$ 所以每个节点开一个 $vector$ 维护祖先到自己所有不同的 $gcd$ 和这个 ...
- CentOS7部署Tomcat服务器
1. 软件 存放路径:/usr/local/src apache-tomcat-9.0.22.tar.gz openjdk-12_linux-x64_bin.tar.gz 2.事先配置 启动后关闭防火 ...
- 什么是 Serverless 应用引擎?优势有哪些?
Serverless 应用引擎(Serverless App Engine,简称 SAE)是面向应用的 Serverless PaaS 平台,能够帮助 PaaS 层用户免运维 IaaS,按需使用,按量 ...
- pc端和移动端的“窗口”(viewport)故事(part1)
A tale of two viewports - part one 在以下的系列文章中,我将为大家解释浏览器中的视窗和一些重要的元素的尺寸是如何起作用的,如:大家最熟悉的html元素以及window ...
- Java排序--排序算法(内排序)
常用内排序算法 我们通常所说的排序算法往往指的是内部排序算法,即需要排序的数据在计算机内存中完成整个排序的过程,当数据率超大或排序较为繁琐时常借助于计算机的硬盘对大数据进行排序工作,称之为外部排序算法 ...
- 剑指offer-数字在排序数组中出现的次数-数组-python
题目描述 统计一个数字在排序数组中出现的次数. python 内置函数 count()一行就能搞定 解题思路 二分查找到给定的数字及其坐标.以该坐标为中点,向前向后找到这个数字的 始 – 终 ...
- Windows2003服务器IIS启用Gzip压缩的设置
http://jingyan.baidu.com/article/148a192178ec834d71c3b12b.html 步骤 1 2 3 本文介绍的HTTP压缩方式,采用的是Window ...
- linux shell 数组的使用
引言 在Linux平台上工作,我们经常需要使用shell来编写一些有用.有意义的脚本程序.有时,会经常使用shell数组.那么,shell中的数组是怎么表现的呢,又是怎么定义的呢?接下来逐一的进行讲解 ...