unity小球酷跑 流程链接:
unity小球酷跑(删减版)_雨木目qq的博客-CSDN博客
https://blog.csdn.net/qq_53603060/article/details/124152196
代码:
cameramove
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameramove : MonoBehaviour {
public GameObject player;
private float offset_c;
// Use this for initialization
void Start () {
offset_c = gameObject.transform.position.x - player.transform.position.x;
}
// Update is called once per frame
void Update () {
followcameramove ();
}
void followcameramove(){
gameObject.transform.position = new Vector3 (offset_c + player.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
}
}
playermove
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class playermove : MonoBehaviour {
public Rigidbody rd;
public float speedautomove=10;
public float speedup = 20;
public GameObject player;
//public int score = 0;
public GameObject down;
public GameObject pass1;
void Start () {
rd=gameObject.GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
//playerautomove ();
playermoveup ();
}
private void playerautomove(){
rd.AddForce (Vector3.right *speedautomove);
}
private void playermoveup(){
float v=Input.GetAxis ("Vertical");
float h=Input.GetAxis("Horizontal");
rd.AddForce (h*Vector3.right*speedup);
//rd.AddForce (h*Vector3.left*speedup);
rd.AddForce (v*Vector3.up*speedup);
//rd.AddForce (v*Vector3.down*speedup);
if (player.transform.position.y > 10 | player.transform.position.y < -10) {
down.SetActive (true);
Time.timeScale = 0;
}
else if (player.transform.position.x > 1000) {
pass1.SetActive (true);
}
}
void OnCollisionEnter(Collision col)
{
var tags = col.collider.transform.tag;
if (col.collider.tag == "barrier") {
//print (2);
UIcontrol._instance.rememberscore (-3);
}
else if (col.collider.tag == "coin") {
//print(3);
UIcontrol._instance.rememberscore (5);
//score_text.text = score.ToString();
//Destroy (col.collider.gameObject);
col.collider.transform.localScale = new Vector3 (0.1f, 0.1f, 0.1f);
}
//else if (col.collider.tag = "wall")
//{
//UIcontrol._instance.rememberscore (0);
//}
}
}
playercolorCollision
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playercolorcolloision : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
private void OnCollisionEnter(Collision collision){
gameObject.GetComponent<Renderer> ().material.color =Color.cyan;
}
private void OnCollisionExit(Collision collision){
gameObject.GetComponent<Renderer> ().material.color =Color.red;
}
}
wallcontrol
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class wallcontrol : MonoBehaviour {
private float offset;
public GameObject player;
public int score = 10;
void Start () {
offset = gameObject.transform.position.x - player.transform.position.x;
}
// Update is called once per frame
void Update () {
Followplayermove();
}
void Followplayermove(){
gameObject.transform.position = new Vector3 (player.transform.position.x + offset, 0, 0);
}
}
barriercontrol
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;// import命名空间。
public class barriercontrol : MonoBehaviour {
//属性
public float interval=10;
public GameObject currentbarrier;
public GameObject barrier_copy;
//引用
public GameObject player;
public GameObject[] barrier_copys;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Autocreatebarrier();//auto barrier create;
//Randombarrierposition();
}
//障碍自动生成
public void Autocreatebarrier()
{
int j=Random.Range(0,barrier_copys.Length-1);
print (j);
barrier_copy=barrier_copys [j];
if(player.transform.position.x>currentbarrier.transform.position.x)
{
//create new barrier
GameObject present=currentbarrier;
//Vector3 target=currentbarrier.transform.position+new Vector3(interval,0,0) 要copy的位置
GameObject g=Instantiate(barrier_copy,currentbarrier.transform.position+new Vector3(interval,0,0),Quaternion.identity);
//判断障碍change
currentbarrier=g;
//print (currentbarrier.transform.color);
Randombarrierposition();
Destroy (present);
}
}
//障碍大小和位置
public void Randombarrierposition()
{
var positions= currentbarrier.transform.position;
var size = currentbarrier.transform.localScale;
float r = Random.Range (-4,3);
float sx = Random.Range (1,3);
float sz = Random.Range (1,3);
float sy = Random.Range (1,3);
currentbarrier.transform.localScale = new Vector3(sx,sy,sz);
currentbarrier.transform.position = new Vector3(positions[0], r, positions[2]);
//Randomsize (5);
}
//障碍位置
public float randomnumber(int m,int n)
{
if (m != n) {
float r = Random.Range (m, n);
return r;
}
else {
return 0;
}
}
}
barriercorol
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class barriercolor : MonoBehaviour {
public Material[] barriersmaterials;
void Start(){
int i = Random.Range(0,barriersmaterials.Length-1);
gameObject.GetComponent<Renderer>().material= barriersmaterials[i];
}
}
UIcontrol
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIcontrol : MonoBehaviour {
public Text scoretext;
public int score;
public GameObject end;
public GameObject pass;
//游戏开发设计模式--单例模式
public static UIcontrol _instance;
public void Awake()
{
_instance = this;
//print(1);
}
public void rememberscore(int x)
{
score += x;
scoretext.text = "得分"+score;
//print(111);
if (score > 55) {
pass.SetActive (true);
Time.timeScale = 0;//player 停止,静止ball里的runing
}
else if (score <-100)
{
end.SetActive (true);
Time.timeScale = 0;
}
}
}
coincontrol
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class coincontrol : MonoBehaviour {
// Use this for initialization
public float interval=15;
public GameObject currentcoin;
public GameObject coin_copy;
//引用
public GameObject player;
void Start () {
}
// Update is called once per frame
void Update () {
Autocreatebarrier ();
}
//生成
public void Autocreatebarrier()
{
if(player.transform.position.x>currentcoin.transform.position.x)
{
GameObject present=currentcoin;
//create new coin
Vector3 targetpos=currentcoin.transform.position+new Vector3(interval,0,0);
GameObject g=Instantiate(coin_copy,targetpos,Quaternion.identity);
//判断障碍change
currentcoin=g;
Randombarrierposition ();
Destroy (present);
}
}
//位置
public void Randombarrierposition()
{
var positions= currentcoin.transform.position;
float r = Random.Range (-1,2);
currentcoin.transform.position = new Vector3(positions[0], r, positions[2]);
}
}