Game
Prefabs
存放预制体,Scripts
脚本,Sounds
声音,Textures
图片资源,Materiais
材质注意:alt+鼠标左键切换视角
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody m_Rigidbody;
// 最大力-public-易于调试
public float fMaxForce = 500.0f;
// 当前力量
private float m_CurForce = 0.0f;
// Start is called before the first frame update
void Start()
{
m_Rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0)) {}
else if (Input.GetMouseButton(0)) // 按下鼠标左键开始蓄力
{
// 一秒的时间内力量增加100*
m_CurForce += Time.deltaTime * 100;
// 最大力量限制
if (m_CurForce > fMaxForce)
{
m_CurForce = fMaxForce;
}
}
else if (Input.GetMouseButtonUp(0))
{
// 调用Jump函数
Jump();
// 跳起后清零
m_CurForce = 0.0f;
}
}
// 跳跃
private void Jump()
{
// 施加向上的力
m_Rigidbody.AddForce(Vector3.up * m_CurForce);
// 施加向前的力
m_Rigidbody.AddForce(Vector3.forward * m_CurForce);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody m_Rigidbody;
// 最大力-public-易于调试
public float fMaxForce = 500.0f;
// 当前力量
private float m_CurForce = 0.0f;
// Start is called before the first frame update
void Start()
{
m_Rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0)) { }
else if (Input.GetMouseButton(0)) // 按下鼠标左键开始蓄力
{
// 一秒的时间内力量增加100*
m_CurForce += Time.deltaTime * 100;
// 最大力量限制
if (m_CurForce > fMaxForce)
{
m_CurForce = fMaxForce;
}
}
else if (Input.GetMouseButtonUp(0))
{
// 调用Jump函数
Jump();
// 跳起后清零
m_CurForce = 0.0f;
}
ShowScale();
}
// 跳跃
private void Jump()
{
// 施加向上的力
m_Rigidbody.AddForce(Vector3.up * m_CurForce);
// 施加向前的力
m_Rigidbody.AddForce(Vector3.forward * m_CurForce);
}
// 蓄力表现
private void ShowScale()
{
float sc = (fMaxForce - m_CurForce * 0.5f) / fMaxForce; // 比值实现蓄力表现:0-不变;1-缩小 m_CurForce * 0.5f:身高最多缩小到一般
Vector3 scale = transform.localScale; // 获取角色当前缩放值
scale.y = sc * 0.2f; // 因为角色原有的缩放比为0.2,保证角色的正常状态
transform.localScale = scale;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody m_Rigidbody;
// 最大力-public-易于调试
public float fMaxForce = 500.0f;
// 当前力量
private float m_CurForce = 0.0f;
// 预制体-产生新的盒子
public GameObject Box = null;
// 新盒子的距离范围(距离必须超过盒子大小)
public float fMaxDistance = 1.2f;
public float fMinDistance = 3.0f;
// 新盒子的高矮
public float fMaxHeight = 0.3f;
public float fMinHeight = 2.0f;
// 新盒子的方向
private Vector3 m_Direction = Vector3.forward; // 默认在前方
// 实际的距离和高矮
private float m_Distance = 0.0f;
private float m_Height = 0.0f;
// Start is called before the first frame update
void Start()
{
m_Rigidbody = GetComponent<Rigidbody>();
// 游戏开始:产生新盒子
GenerateBox();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0)) { }
else if (Input.GetMouseButton(0)) // 按下鼠标左键开始蓄力
{
// 一秒的时间内力量增加100*
m_CurForce += Time.deltaTime * 100;
// 最大力量限制
if (m_CurForce > fMaxForce)
{
m_CurForce = fMaxForce;
}
}
else if (Input.GetMouseButtonUp(0))
{
// 调用Jump函数
Jump();
// 跳起后清零
m_CurForce = 0.0f;
}
ShowScale();
}
// 跳跃
private void Jump()
{
// 施加向上的力
m_Rigidbody.AddForce(Vector3.up * m_CurForce);
// 施加向前的力
m_Rigidbody.AddForce(Vector3.forward * m_CurForce);
}
// 蓄力表现
private void ShowScale()
{
float sc = (fMaxForce - m_CurForce * 0.5f) / fMaxForce; // 比值实现蓄力表现:0-不变;1-缩小 m_CurForce * 0.5f:身高最多缩小到一般
Vector3 scale = transform.localScale; // 获取角色当前缩放值
scale.y = sc * 0.2f; // 因为角色原有的缩放比为0.2,保证角色的正常状态
transform.localScale = scale;
}
private GameObject GenerateBox()
{
// 创建新盒子
GameObject obj = GameObject.Instantiate(Box);
// 随机生成参数
m_Distance = Random.Range(fMinDistance, fMaxDistance);
m_Height = Random.Range(fMinHeight, fMaxHeight);
m_Direction = Random.Range(0, 2) == 1 ? Vector3.forward : Vector3.left; // 1:前方 2:左
// 方向*距离+目前角色位置
Vector3 pos = m_Direction * m_Distance + transform.position; // 角色悬空导致盒子悬空
pos.y = 0; // 盒子落地
// 更新盒子的位置
obj.transform.position = pos;
// 更新盒子的高度:对x,z轴不进行改变,只对y轴进行改变
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 null;
}
}
m_Rigidbody.AddForce(m_Direction * m_CurForce);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody m_Rigidbody;
// 最大力-public-易于调试
public float fMaxForce = 500.0f;
// 当前力量
private float m_CurForce = 0.0f;
// 预制体-产生新的盒子
public GameObject Box = null;
// 新盒子的距离范围(距离必须超过盒子大小)
public float fMaxDistance = 1.2f;
public float fMinDistance = 3.0f;
// 新盒子的高矮
public float fMaxHeight = 0.3f;
public float fMinHeight = 2.0f;
// 新盒子的方向
private Vector3 m_Direction = Vector3.forward; // 默认在前方
// 实际的距离和高矮
private float m_Distance = 0.0f;
private float m_Height = 0.0f;
// Start is called before the first frame update
void Start()
{
m_Rigidbody = GetComponent<Rigidbody>();
// 游戏开始:产生新盒子
GenerateBox();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0)) { }
else if (Input.GetMouseButton(0)) // 按下鼠标左键开始蓄力
{
// 一秒的时间内力量增加100*
m_CurForce += Time.deltaTime * 100;
// 最大力量限制
if (m_CurForce > fMaxForce)
{
m_CurForce = fMaxForce;
}
}
else if (Input.GetMouseButtonUp(0))
{
// 调用Jump函数
Jump();
// 跳起后清零
m_CurForce = 0.0f;
}
ShowScale();
}
// 跳跃
private void Jump()
{
// 施加向上的力
m_Rigidbody.AddForce(Vector3.up * m_CurForce);
// 施加向前的力
m_Rigidbody.AddForce(m_Direction * m_CurForce);
}
// 蓄力表现
private void ShowScale()
{
float sc = (fMaxForce - m_CurForce * 0.5f) / fMaxForce; // 比值实现蓄力表现:0-不变;1-缩小 m_CurForce * 0.5f:身高最多缩小到一般
Vector3 scale = transform.localScale; // 获取角色当前缩放值
scale.y = sc * 0.2f; // 因为角色原有的缩放比为0.2,保证角色的正常状态
transform.localScale = scale;
}
private GameObject GenerateBox()
{
// 创建新盒子
GameObject obj = GameObject.Instantiate(Box);
// 随机生成参数
m_Distance = Random.Range(fMinDistance, fMaxDistance);
m_Height = Random.Range(fMinHeight, fMaxHeight);
m_Direction = Random.Range(0, 2) == 1 ? Vector3.forward : Vector3.left; // 1:前方 2:左
// 方向*距离+目前角色位置
Vector3 pos = m_Direction * m_Distance + transform.position; // 角色悬空导致盒子悬空
pos.y = 2.0f; // 盒子落地
// 更新盒子的位置
obj.transform.position = pos;
// 更新盒子的高度:对x,z轴不进行改变,只对y轴进行改变
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 null;
}
}