1.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player1 : MonoBehaviour
{
private Rigidbody m_RigidBody;
public float fMaxForce = 500.0f;
private float m_CurForce = 80f;
public GameObject Box = null;
public float fMinDistance = 1.2f;//克隆的盒子离预制体盒子的最小距离;
public float fMaxDistance = 3.0f;//克隆的盒子离预制体盒子的最大距离;
public float fMinHeiget =0.3f;//克隆的盒子离预制体盒子的最矮
public float fMaxHeiget = 2.0f;//克隆的盒子离预制体盒子的最高;
public Vector3 m_Driection = Vector3.forward;//克隆的盒子在前方
private float m_Distance = 0.0f;//盒子生成的实际距离
private float m_Height = 0.0f;//盒子生成的实际高度
private GameObject m_CurCube = null;//当前立方体
private GameObject m_NextCube = null;//下一个立方体
private Vector3 m_Camera = Vector3.zero;
private GameObject m_Plane = null;
private Animator m_Animator = null;//获取当前动画控制器
private UIM m_UI = null;
// Start is called before the first frame update
void Start()
{
m_RigidBody = GetComponent<Rigidbody>();
m_Animator = GetComponent<Animator>();//获取当前动画控制器
m_UI = GetComponent<UIM>();
m_Plane = GameObject.FindGameObjectWithTag("Plane");
m_NextCube = GenerateBox();
}
// Update is called once per frame
void Update()
{
GameObject obj = GetObject();
if (obj != null)
{
if (obj.tag == "Cube")//检查是否落在立方体上面
{
if (m_CurCube == null) //说明游戏刚刚掉落到立方体上面,游戏刚开始
{
m_CurCube = obj;//游戏刚开始
m_Camera = Camera.main.transform.position - m_CurCube.transform.position;//相机坐标减去当前立方体坐标
}
else if (m_NextCube==obj)
{
m_UI.AddScore(1);
//Destroy(m_CurCube);//3.销毁原来的立方体
m_CurCube = m_NextCube;//1.把当前立方体变成下一个立方体,这样不会一直加分
m_NextCube = GenerateBox();//2.生成新的立方体
m_RigidBody.Sleep();//让刚体休眠一帧,防止物体位移滑动
m_RigidBody.WakeUp();//唤醒
m_Animator.SetBool("Forward", false);
m_Animator.SetBool("Left", false);
}
ProcessInput();
ShowScale();
CameMove();
}
else
{
m_UI.SetGameOver(true);
}
}
}
private void ProcessInput()
{
if (Input.GetMouseButtonDown(0))
{
}
else if (Input.GetMouseButton(0))
{
m_CurForce += Time.deltaTime * 100;
if (m_CurForce > fMaxForce)
m_CurForce = fMaxForce;//限制最大力量
}
else if (Input.GetMouseButtonUp(0))
{
Jump();
m_CurForce = 0.0f;//力量清零
}
}
private void CameMove()//摄像机跟随主角平滑移动
{
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position,
m_CurCube.transform.position + m_Camera, Time.deltaTime * 2);
Vector3 pos = m_CurCube.transform.position;
pos.y = 0;
m_Plane.transform.position = pos;
}
private void ShowScale() //蓄力表现函数
{
float sc =(fMaxForce- m_CurForce*0.5f) / fMaxForce;
Vector3 scale = transform.localScale;//获取坐标值
scale.y = sc * 1f;
transform.localScale = scale;
}
private void Jump() //跳高的同时往前跳
{
m_RigidBody.AddForce(Vector3.up * m_CurForce);//跳跃高度
m_RigidBody.AddForce(m_Driection * m_CurForce);//往前跳
if (m_Driection == Vector3.forward)//播放向前跳跃动画
m_Animator.SetBool("Forward", true);
else
m_Animator.SetBool("Left", true);
}
private GameObject GenerateBox() //随机生成盒子
{
GameObject obj = GameObject.Instantiate(Box);//创建盒子
m_Distance = Random.Range(fMinDistance, fMaxDistance);//随机距离
m_Height = Random.Range(fMinHeiget, fMaxHeiget);//随机大小
m_Driection = Random.Range(0, 2) == 1 ? Vector3.forward : Vector3.left;//随机方向
Vector3 pos = m_Driection * m_Distance + transform.position;
pos.y = fMinHeiget*0.5f;//随机出现在y轴上
obj.transform.position = pos;
obj.transform.localScale = new Vector3(1, m_Height, 1);
obj.GetComponent<MeshRenderer>().material.color = new Color(Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f));
//更改材质属性里面的颜色,可以生成随机颜色
return obj;
}
private GameObject GetObject()//碰撞物体射线检测
{
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit, 0.3f))
{
//Debug.Log(hit.collider.tag);
return hit.collider.gameObject;
}
else
{
Vector3[] vOffests = { Vector3.forward, Vector3.back, Vector3.left, Vector3.right };
foreach (Vector3 vof in vOffests)
{
if (Physics.Raycast(transform.position+vof*0.1f, Vector3.down, out hit, 2f))
{
//Debug.Log(hit.collider.tag);
return hit.collider.gameObject;
}
}
}
return null;
}
}
2.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class UIM : MonoBehaviour
{
public Text Score;
public Text BestScore;
public Text GameOver;
private int m_nScore = 0;
private int m_BestScore = 0;
// Start is called before the first frame update
void Start()
{
GameOver.enabled = false;
m_nScore = 0;//初始成绩为0
m_BestScore = PlayerPrefs.GetInt("BestScore");//加载最好成绩
RefershScorce();
}
void RefershScorce()
{
string str = string.Format("当前成绩{0}", m_nScore);
Score.text = str;
str = string.Format("最好成绩{0}", m_BestScore);
BestScore.text = str;
}
public void AddScore(int sc=1)
{
m_nScore += sc;
if (m_nScore > m_BestScore)
{
m_BestScore = m_nScore;
PlayerPrefs.SetInt("BestScore", m_BestScore);
}
RefershScorce();
}
public void SetGameOver(bool isEnd=true)
{
GameOver.enabled = isEnd;
}
// Update is called once per frame
public void OnReStart()
{
SceneManager.LoadScene("Jump and Iump");
}
public void OnQuit()
{
// Application.Quit();
SceneManager.LoadScene(0);
}
}
人物控件如图: