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=1;
public float MaxDistance = 4;
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;
private int _score;
Vector3 _direction = new Vector3(1, 0, 0);
// Use this for initialization
void Start () {
_rigidbody = GetComponent<Rigidbody>();
Particle = GameObject.Find("yellow");
Particle.SetActive(false);
/
/修改物理组件的重⼼到body的底部
_OfMass = ;
_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);
_ansform.DOLocalMoveY(0.25f,0.2f);
_ansform.DOScale(new Vector3(1,0.5f,1),0.2f);
}
if (Input.GetKey(KeyCode.Space))
{
//盒⼦缩放沿着轴⼼缩放
_ansform.localScale += new Vector3(0, -1, 0)*0.15f*Time.deltaTime;
_ansform.localPosition += new Vector3(0, -1, 0) * 0.15f * Time.deltaTime;
}
}
void OnJump(float elapse)
{
_rigidbody.AddForce((new Vector3(0,1,0)+_direction)*elapse* Factoer,ForceMode.Impulse);
}
void SpawnStage()
{
var stage = Instantiate(Stage);
var randomScale = Random.Range(0.5f,1);
stage.GetComponent<Renderer>().lor = new Color(Random.Range(0.01f, 1), Random.Range(0.01f, 1), Random.Range(0.01f, 1)); }
//如果有别的物体和本物体发⽣变化,会触发这函数
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.name.Contains("Stage") && llider!=_lastCollisionCollider)
{
_lastCollisionCollider = llider;
_currentStage = collision.gameObject;
RandomDirection();
SpawnStage();
MoveCamera();
_score++;
< = _score.ToString();
}
if(collision.gameObject.name=="Ground")
positionrelative啥意思{
//本局游戏结束,重新开始
SceneManager.LoadScene("Gary");
}
}
void RandomDirection()
{
var seed = Random.Range(0,2);
if(seed == 0)
{
_direction = new Vector3(1, 0, 0);
}
else
{
_direction = new Vector3(0,0,1);
}
}
void MoveCamera()
{
Camera.DOMove(transform.position + _camerRelativePosition,1);
}
}
Player.cs
Player.cs
程序已放到Github上托管(⾥⾯有个32bit可执⾏⽂件):
⼩游戏没有⽤到素材、资源包(DOTween插件不算)
⼀个脚本
实现过程
【脚本中public外部引⽤的控件需要⾃⼰拖拽到相应位置上】
创建⼀个3D场景
创建⼀个Cube,y轴缩放0.25当跳板,创建物体可以通过Transsform中Reset设置为3D场景正中⼼,在创建⼀个Plane当地⾯,跳⼀跳⼩⼈物碰到地⾯时游戏借宿,为了让跳板出现地⾯上⽅,设置y轴Po
stion为0.25(Reset设置居中是以组件正中⼼)
创建⼀个材质球改变地⾯颜⾊(默认Pllane控件是不能设置材质颜⾊)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论