跳一跳
工程文件界面
游戏界面
脚本
usingDG.Tweening;usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.SceneManagement;usingUnityEngine.UI;public classPlayer : MonoBehaviour {public float Factoer=1;public float MaxDistance = 4;publicGameObject Stage;//获取相机位置
publicTransform Camera;privateRigidbody _rigidbody;private float_stateTime;publicGameObject Particle;publicTransform Head;publicTransform Body;//当前盒子物体
privateGameObject _currentStage;privateCollider _lastCollisionCollider;//相机的相对位置
publicVector3 _camerRelativePosition;publicText ScoreText;private int_score;
Vector3 _direction= new Vector3(1, 0, 0);//Use this for initialization
voidStart () {
_rigidbody= GetComponent();
Particle= GameObject.Find("yellow");
Particle.SetActive(false);//修改物理组件的重心到body的底部
_rigidbody.centerOfMass =Vector3.zero;
_currentStage=Stage;
_lastCollisionCollider= _currentStage.GetComponent();
SpawnStage();
_camerRelativePosition= Camera.position -transform.position;
}//Update is called once per frame
voidUpdate () {if(Input.GetKeyDown(KeyCode.Space))
{
_stateTime=Time.time;
Particle.SetActive(true);
}if(Input.GetKeyUp(KeyCode.Space))
{var elapse = Time.time -_stateTime;
OnJump(elapse);
Particle.SetActive(false);
Body.transform.DOScale(0.1f,1);
Head.transform.DOLocalMoveY(0.29f,0.5f);
_currentStage.transform.DOLocalMoveY(0.25f,0.2f);
_currentStage.transform.DOScale(new Vector3(1,0.5f,1),0.2f);
}if(Input.GetKey(KeyCode.Space))
{
Body.transform.localScale+= new Vector3(1, -1, 1) * 0.05f *Time.deltaTime;
Head.transform.localPosition+= new Vector3(0,-1,0)*0.1f*Time.deltaTime;//盒子缩放沿着轴心缩放
_currentStage.transform.localScale += new Vector3(0, -1, 0)*0.15f*Time.deltaTime;
_currentStage.transform.localPosition+= new Vector3(0, -1, 0) * 0.15f *Time.deltaTime;
}
}void OnJump(floatelapse)
{
_rigidbody.AddForce((new Vector3(0,1,0)+_direction)*elapse*Factoer,ForceMode.Impulse);
}voidSpawnStage()
{var stage =Instantiate(Stage);
stage.transform.position= _currentStage.transform.position + _direction * Random.Range(1.1f,MaxDistance) ;var randomScale = Random.Range(0.5f,1);
Stage.transform.localScale= new Vector3(randomScale, 0.5f, randomScale);
stage.GetComponent().material.color = new Color(Random.Range(0.01f, 1), Random.Range(0.01f, 1), Random.Range(0.01f, 1));
}//如果有别的物体和本物体发生变化,会触发这函数
voidOnCollisionEnter(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");
}
}voidRandomDirection()
{var seed = Random.Range(0,2);if(seed == 0)
{
_direction= new Vector3(1, 0, 0);
}else{
_direction= new Vector3(0,0,1);
}
}voidMoveCamera()
{
Camera.DOMove(transform.position+ _camerRelativePosition,1);
}
}
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
获得组件
privateRigidbody _rigidbody;voidStart () {
_rigidbody= GetComponent();
}
计算出按下空格(space)和松开空格之间的时间
private float_stateTime;voidUpdate () {if(Input.GetKeyDown(KeyCode.Space))
{
_stateTime=Time.time;
}if(Input.GetKeyUp(KeyCode.Space))
{var elapse = Time.time -_stateTime;
OnJump(elapse);
}
}
Player主键跳跃的距离
public float Factoer=1;void OnJump(floatelapse)
{
_rigidbody.AddForce(new Vector3(1,1,0)*elapse*Factoer,ForceMode.Impulse);
}
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;public classPlayer : MonoBehaviour {public float Factoer=1;privateRigidbody _rigidbody;private float_stateTime;//Use this for initialization
voidStart () {
_rigidbody= GetComponent();
}//Update is called once per frame
voidUpdate () {if(Input.GetKeyDown(KeyCode.Space))
{
_stateTime=Time.time;
}if(Input.GetKeyUp(KeyCode.Space))
{var elapse = Time.time -_stateTime;
OnJump(elapse);
}
}void OnJump(floatelapse)
{
_rigidbody.AddForce(new Vector3(1,1,0)*elapse*Factoer,ForceMode.Impulse);
}
}
Player.cs
发现人物跳跃落地时会出现跌倒,这是因为Body底部是圆的
将Body下的Capsle Collider去掉,替换成Box Collider
通过Player.cs修改Player
voidStart () {
_rigidbody= GetComponent();//修改物理组件的重心到body的底部
_rigidbody.centerOfMass =Vector3.zero;
}
修改当前视角,选中Main Camera按下快捷键Ctrl+Alt+F (GameObject->Align With->View)
修改Factoer为3,测试(这个盒子是我自己复制Stage出来的)
盒子自动生成
随机生成盒子位置
stage.transform.position = _currentStage.transform.position + new Vector3(Random.Range(1.1f,MaxDistance),0,0);
privateRigidbody _rigidbody;private float_stateTime;//当前盒子物体
privateGameObject _currentStage;voidStart () {
_rigidbody= GetComponent();//修改物理组件的重心到body的底部
_rigidbody.centerOfMass =Vector3.zero;
_currentStage=Stage;
SpawnStage();
}voidSpawnStage()
{var stage =Instantiate(Stage);
stage.transform.position= _currentStage.transfor