小球酷跑unity
**1.环境搭建和移动就是两个长方体中间夹着一个小球然后让小球实现上下移动以及自动向右边行驶的代码如下
**
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
public float speedAutoMove = 5;
public float speedMoveUpAndDown = 5;
private void Update()
{
PlayerAutoMove();
PlayerMoveUpAndDown();
PlayerMoveMaxSpeed();
}
public void PlayerAutoMove()
{
gameObject.GetComponent<Rigidbody>().AddForce(Vector3.right*speedAutoMove);
}
public void PlayerMoveUpAndDown()
{
float v = Input.GetAxis("Vertical");
gameObject.GetComponent<Rigidbody>().AddForce(Vector3.up * speedMoveUpAndDown*v);
}
public void PlayerMoveMaxSpeed()
{
gameObject.GetComponent<Rigidbody>().velocity = new Vector3(5, gameObject.GetComponent<Rigidbody>().velocity. y, 0);
}
}
接下来实现相机跟随小球运动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControl : MonoBehaviour
{
// Start is called before the first frame update
private float offset;
public GameObject player;
void Start()
{
offset = gameObject.transform.position.x - player.transform.position.x;
}
// Update is called once per frame
void Update()
{
FllowPlayer();
}
public void FllowPlayer()
{
gameObject.transform.position = new Vector3(offset + player.transform.position.x,gameObject.transform.position.y, gameObject.transform.position.z);
}
}
以及墙体跟随小球运动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallControl : MonoBehaviour
{
private float offset;
public GameObject player;
private void Start()
{
offset = gameObject.transform.position.x - player.transform.position.x;
}
private void Update()
{
FllowPlayerMove();
}
public void FllowPlayerMove()
{
gameObject.transform.position = new Vector3(player.transform.position.x+offset, gameObject.transform.position.y, gameObject.transform.position.z);
}
}
2.障碍自动生成前期准备
首先建立一个障碍物barrier障碍物根据坐标进行障碍的自动生成同时将障碍作为预制体`
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BarrierControl : MonoBehaviour
{
// Start is called before the first frame update
public float barrierInterval = 5;
public GameObject player;
public GameObject CurrentBarrier;
public GameObject barrierPerfab;
void Start()
{
}
// Update is called once per frame
void Update()
{
AutoCreateBarriers();
}
public void AutoCreateBarriers()
{
if (player.transform.position.x > CurrentBarrier.transform.position.x)
{
UIContol._instance.AddScore(5);
float targetX= CurrentBarrier.transform.position.x+ barrierInterval;
float targetY= RandomBarrierPosition();
Vector3 targetPos = new Vector3(targetX,targetY,0);
GameObject g= Instantiate(barrierPerfab,targetPos,Quaternion.identity);
g.transform.localScale = new Vector3 (g.transform.localScale.x,RandomBarrierSize((int)g.transform.position.y),g.transform.localScale.z);
CurrentBarrier = g;
}
}
public float RandomBarrierSize(int r)
{
int rAbs = Mathf.Abs(r);
if (rAbs == 0)
{
return Random.Range(0,8);
}
else
{
return Random.Range(1, ((4 - rAbs) * 2) + 1);
}
}
public float RandomBarrierPosition()
{
int r = Random.Range(-4, 5);
return r;
}
}
3.自动销毁障碍
设置一个透明的长方体使其可以进行触发勾选is trigger选项使他触发时销毁方块代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AutoDestroyBarriers : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
Destroy(other.gameObject);
}
}
接下来实现障碍的随机颜色
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Barrier : MonoBehaviour
{
// Start is called before the first frame update
public Material[] barrierMaterials;
void Start()
{
int i = Random.Range(0, barrierMaterials.Length);
gameObject.GetComponent<Renderer>().material = barrierMaterials[i];
}
// Update is called once per frame
void Update()
{
}
}
4.碰到障碍提示
进行加减分以及颜色变换
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerCollision : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
UIContol. _instance.AddScore(-3);
gameObject.GetComponent<Renderer>().material.color = Color.red;
}
private void OnCollisionExit(Collision collision)
{
gameObject.GetComponent<Renderer>().material.color = Color.white;
}
}
新建canvas建立计分板
using UnityEngine.UI;
public class UIContol : MonoBehaviour
{
public Text scoreText;
public int score=0;
public static UIContol _instance;
private void Awake()
{
_instance = this;
}
public void AddScore(int x)
{
score += x;
scoreText.text = "得分:" + score;
}
}
同时优化了小球i速度
public void PlayerMoveMaxSpeed()
{
gameObject.GetComponent<Rigidbody>().velocity = new Vector3(5, gameObject.GetComponent<Rigidbody>().velocity. y, 0);
}
5。设置开始界面
进行按钮设置
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
// Start is called before the first frame update
public void OnStartGame(string sceneName)
{
SceneManager.LoadScene(sceneName);
}
}
使其点击开始游戏跳转场景
最后封装两个场景
大功告成