Roll a ball游戏学习总结。

时间:2021-03-03 03:31:12

今天主要是学做roll a ball来进行unity的入门学习,了解它的一些简单操作。

首先是1工程的创建,2创建地面和游戏者a ball,注意创建GameObject后对Transform进行reset。3移动控制。代码如下:

public float speed;
void FixedUpdate(){
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 moveMent
= new Vector3 (moveHorizontal, 0.0f, moveVertical);
GetComponent
<Rigidbody> ().AddForce (moveMent * speed * Time.deltaTime);
}

两点需要注意:一个是要写在FixeUpdate中,FixeUpdate是基于物理时间的调用,常用于控制运动。一个是注意将代码添加到a ball上后,要对speed进行赋值,这里我用的是500.

还有通过力来控制物体会显得更加真实。

4做围墙。5做摄影机的跟随。代码如下:

        public GameObject player;
private Vector3 Offset;

// Use this for initialization
void Start () {
Offset
= transform.position - player.transform.position;

}

// Update is called once per frame
void Update () {

}
void LateUpdate(){
transform.position
= player.transform.position + Offset;
}

同样代码添加完后,要对player进行赋值。值为小球物体。

6食物的创建,使用预制体方便,注意要reset,再进行食物旋转的代码编写,代码如下:

 
 

void Start () {

}
void FixedUpdate(){
transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime);
}

 

注意所有的食物属性的更改都只对预制体操作,还有运动需写在FixedUpdate中。7对预制体设置标签,用于计数。

8创建UI界面,在移动控制代码中添加计分功能,显示出来,还加了一个胜利的判断。

9运行调试,最后导出。

接下来是自己更改后的小球二代。

改进为小球为一定时间间隔内自动生成,且位置为随机的,玩法也有改进,必须按照小球的生成顺序吃,才能吃球,生成小球间隔时间为自己可调节的。附加代码有两部分,一部分是在角色控制中加的判断,另一部分是自己之前写的单链表,因为判断顺序吃球中有用到单链表。代码如下

主角控制代码:

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

public class PlayerController : MonoBehaviour {

// Use this for initialization
public float speed;
private int count;
public Text countText;
public int Max;
//
public GameObject Prefab;
private float cubeX;
private float cubeZ;
public float jiange;
private float t1;
private float t2;


TestList
<GameObject>obj = new TestList<GameObject>();


void Start(){
t1
= 0.0f;
t2
= 0.0f;
count
= 0;
setCountText ();


}
void FixedUpdate(){
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 moveMent
= new Vector3 (moveHorizontal, 0.0f, moveVertical);
GetComponent
<Rigidbody> ().AddForce (moveMent * speed * Time.deltaTime);

}

// Update is called once per frame
void Update () {
Produce ();

}
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "PickUp"&&obj.GetIndex(other.gameObject)==count+1) {
other.gameObject.SetActive (
false);
count
++;
setCountText ();


}
}
void setCountText(){
if (count < Max) {
countText.text
= "Count:" + count;
}
else {
countText.text
= "You Win!";
}
}
void Produce(){
if (t2-t1 < jiange) {
t2
+= Time.deltaTime;
}
else {
t1
= t1 + jiange;
t2
+= Time.deltaTime;

GameObject cube
= (GameObject)Instantiate (Prefab);
obj.Add (cube);
cubeX
= Random.Range (-10.0f, 10.0f);
cubeZ
= Random.Range (-10.0f, 10.0f);

Vector3 weizhi
= new Vector3 (cubeX, 0.5f, cubeZ);
cube.transform.position
= weizhi;
}

}
}

单链表代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace dataLianbiao{


public interface LianbiaoList<T>{
void Add (T item);
int Count{ get;}
void Clear ();
void Insert (int index, T item);
void RemoveAt (int index);
T GetValue (
int indexer);
int GetIndex (T item);
bool IsEmpty ();




}




class Node<T>{
public T Value;
public Node<T>NextNode;
public Node(T value){
Value
= value;
NextNode
= null;
}
public Node(){
Value
= default(T);
}
}
class TestList<T>:LianbiaoList<T>{
private Node<T>head = null;
private int _count = 0;
private int maxSize =100;
public bool IsEmpty(){
return _count > maxSize;
}
public void Add(T item){

if (head == null) {
head
= new Node<T> (item);
_count
++;
}
else {
Node
<T> node = head;

while (node.NextNode != null) {
node
= node.NextNode;
}
_count
++;
node.NextNode
= new Node<T> (item);

}


}
public int Count{get{ return _count;}}
public void Clear(){
head
= null;
_count
= 0;
}
public void Insert(int index,T item){
if (index >= 0 && index < _count) {
Node
<T> node = head;
Node
<T> prev = null;
Node
<T> next = null;
if (index == 0) {
next
= head;
head
= new Node<T> (item);
head.NextNode
= next;
}
else {
for (int i = 0; i < index; i++) {
prev
= node;
node
= node.NextNode;
}
next
= node;
node
= new Node<T> (item);
node.NextNode
= next;
prev.NextNode
= node;
}
_count
++;
}
/*else {
throw new UnityException ("Out of Range!");
}
*/
}
public void RemoveAt(int index){
if (index >= 0 && index < _count) {
Node
<T> node = head;
Node
<T> prev = null;
if (index == 0) {
head
= head.NextNode;
}
else {
for (int i = 0; i < index; i++) {
prev
= node;
node
= node.NextNode;
}

prev.NextNode
= node.NextNode;
}
_count
--;
}
}
public IEnumerator<T> GetEnumerator(){
Node
<T> node = head;
Node
<T> result = new Node<T> ();
while (node != null) {
result
= node;
node
= node.NextNode;
yield return result.Value;
}
}
public T GetValue(int index){
if (index >= 0 && index < _count) {
Node
<T> node = head;
for (int i = 1; i < index; i++) {
node
= node.NextNode;
}
return node.Value;
}
return default(T);
}
public int GetIndex(T item){
Node
<T> node = head;
int i = 1;
while (!node.Value.Equals (item) && node.NextNode != null) {
node
= node.NextNode;
i
++;
}
if (i == _count) {
return -1;
}
else {
return i;
}



}

}



}

后面开始尝试UI的学习。