# Unity开发微信跳一跳小游戏

13 篇文章 3 订阅

unity开发微信跳一跳

player完整脚本

## 准备人物、地板、小方块

可以搞个材料上色，没有颜色也没关系，我一开始先不管，颜色是后面添加材料有的。

## 实现摄像头跟随

using UnityEngine;

public class follow_player : MonoBehaviour
{
public Transform player;
public Vector3 offset;

private void FixedUpdate()
{
transform.position = player.position + offset;
}
}

## 实现地板移动

using UnityEngine;

public class Ground : MonoBehaviour
{
public Transform player;

// Update is called once per frame
void Update()
{
var position = player.position;
transform.position = new Vector3(position.x, 0, position.z);
}
}

## 实现小方块自动生成

    private void NewCube()
{
var random = new System.Random();
direction = random.Next(0, 2);
if (direction == 0)
{
var cube = Instantiate(cubePrefab, new Vector3(Random.Range(10, 13), 0, 0) + cubePlace,
Quaternion.identity);
cubePlace = cube.transform.position;
cubes.Enqueue(cube);
}
else
{
var cube = Instantiate(cubePrefab, new Vector3(0, 0, Random.Range(10, 13)) + cubePlace,
Quaternion.identity);
cubePlace = cube.transform.position;
cubes.Enqueue(cube);
}

if (cubes.Count > maxCubeNumber)
{
var cube = cubes.Dequeue();
Destroy(cube);
}
}

## 实现小人跳动

            var touch = Input.touches[0];
if (touch.phase == TouchPhase.Began)
{
start = Time.time;
}
else if (touch.phase == TouchPhase.Ended)
{
var time = Time.time - start;

if (direction == 0)
{
playerRigidbody.AddForce(new Vector3(1, 1, 0) * (time * speed), ForceMode.Impulse);
}
else
{
playerRigidbody.AddForce(new Vector3(0, 1, 1) * (time * speed), ForceMode.Impulse);
}

}

## 实现小人伸缩

        if (Input.touchCount > 0 && inTheAir == false)
{
jump = true;
if (body.transform.localScale.y > 0.5)
{
body.transform.localScale += new Vector3(1, -1, 1) * (0.45f * Time.deltaTime);
head.transform.localPosition += new Vector3(0, -1, 0) * (0.45f * Time.deltaTime);
}

var touch = Input.touches[0];
if (touch.phase == TouchPhase.Began)
{
start = Time.time;
}
else if (touch.phase == TouchPhase.Ended)
{
var time = Time.time - start;
if (time > 3)
{
time = 3;
}else if (time < 0.5f)
{
time = 0.5f;
}

if (direction == 0)
{
playerRigidbody.AddForce(new Vector3(1, 1, 0) * (time * speed), ForceMode.Impulse);
}
else
{
playerRigidbody.AddForce(new Vector3(0, 1, 1) * (time * speed), ForceMode.Impulse);
}

body.transform.localScale = new Vector3(1, 1.5f, 1);
head.transform.localPosition = new Vector3(0, 3.7f, 0);
inTheAir = true;
}
}

## 实现小方块自动销毁

        if (cubes.Count > maxCubeNumber)
{
var cube = cubes.Dequeue();
Destroy(cube);
}

## 实现小方块随机大小和随机颜色

using UnityEngine;

public class Cube : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value);
var radius = Random.Range(4, 7);
}
}

## 实现分数显示

把text的位置整到左上角

        scoreText.text = score.ToString();

## 实现跌落重玩

    private void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Ground") || other.gameObject == currentCube && jump)
{
// FindObjectOfType<GameManager>().EndGame();
}
else if (other.gameObject != currentCube)
{
score += 1;
inTheAir = false;
NewCube();
currentCube = other.gameObject;
}
}

## player完整脚本

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Play : MonoBehaviour
{
public Rigidbody playerRigidbody;
public GameObject body;
public GameObject cubePrefab;
public Vector3 cubePlace;
private Queue<GameObject> cubes = new Queue<GameObject>();
public int speed = 10;
public int direction = 0;
public int maxCubeNumber = 10;
public Transform massCenter;
public GameObject currentCube;
public Text scoreText;
public int score = 1;
private bool jump = false;
private bool inTheAir = false;
private float start = 0;

// Start is called before the first frame update
void Start()
{
var cube = Instantiate(cubePrefab, cubePlace, Quaternion.identity);
cubes.Enqueue(currentCube);
cubes.Enqueue(cube);
GetComponent<Rigidbody>().centerOfMass = massCenter.localPosition;
}

private void FixedUpdate()
{
if (Input.touchCount > 0 && inTheAir == false)
{
jump = true;
if (body.transform.localScale.y > 0.5)
{
body.transform.localScale += new Vector3(1, -1, 1) * (0.45f * Time.deltaTime);
head.transform.localPosition += new Vector3(0, -1, 0) * (0.45f * Time.deltaTime);
}

var touch = Input.touches[0];
if (touch.phase == TouchPhase.Began)
{
start = Time.time;
}
else if (touch.phase == TouchPhase.Ended)
{
var time = Time.time - start;
if (time > 3)
{
time = 3;
}else if (time < 0.5f)
{
time = 0.5f;
}

if (direction == 0)
{
playerRigidbody.AddForce(new Vector3(1, 1, 0) * (time * speed), ForceMode.Impulse);
}
else
{
playerRigidbody.AddForce(new Vector3(0, 1, 1) * (time * speed), ForceMode.Impulse);
}

body.transform.localScale = new Vector3(1, 1.5f, 1);
head.transform.localPosition = new Vector3(0, 3.7f, 0);
inTheAir = true;
}
}

scoreText.text = score.ToString();
}

private void NewCube()
{
var random = new System.Random();
direction = random.Next(0, 2);
if (direction == 0)
{
var cube = Instantiate(cubePrefab, new Vector3(Random.Range(10, 13), 0, 0) + cubePlace,
Quaternion.identity);
cubePlace = cube.transform.position;
cubes.Enqueue(cube);
}
else
{
var cube = Instantiate(cubePrefab, new Vector3(0, 0, Random.Range(10, 13)) + cubePlace,
Quaternion.identity);
cubePlace = cube.transform.position;
cubes.Enqueue(cube);
}

if (cubes.Count > maxCubeNumber)
{
var cube = cubes.Dequeue();
Destroy(cube);
}
}

private void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Ground") || other.gameObject == currentCube && jump)
{
// FindObjectOfType<GameManager>().EndGame();
}
else if (other.gameObject != currentCube)
{
score += 1;
inTheAir = false;
NewCube();
currentCube = other.gameObject;
}
}
}

• 1
点赞
• 15
收藏
觉得还不错? 一键收藏
• 打赏
• 0
评论
04-15
04-28
08-13
01-30 7360
11-07 1万+
03-24 9604
08-26 1万+
06-01 1万+
03-22 1615
04-17 5581
04-22 2万+

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

MaolinYe（叶茂林）

¥1 ¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。