unity之滚动的小球

导入资源包

在这里插入图片描述

搭建初始场景

在这里插入图片描述

创建黄色方块转动

在这里插入图片描述
转动代码:

public class RotatePickUp : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		this.transform.Rotate(new Vector3(15,30,45)*Time.deltaTime);
	}
}

创建黄色滚动圆环

step1:创建黄色方块并放入materials->Prefabs

step2:

自动生成12个围绕sphere的方块,外加摄像机自动跟随sphere。

代码拖到main camera上面

public class CameraController : MonoBehaviour {
	public GameObject player;
	private Vector3 offset;
	public GameObject pickupPfb;
	private GameObject[] obj1;
	private int objCount = 0;

	// Use this for initialization
	void Start () {
		offset = this.transform.position - player.transform.position;
		obj1 = new GameObject[12];
		for (objCount = 0;objCount < 12 ;objCount++){
			obj1[objCount] = GameObject.Instantiate(pickupPfb);
			obj1[objCount].name = "pickup" + objCount.ToString();
			obj1[objCount].transform.position = new Vector3(4*Mathf.Sin(Mathf.PI/6*objCount),1,4*Mathf.Cos(Mathf.PI/6*objCount));
		}

	}
	void LateUpdate() {
		this.transform.position= player.transform.position + offset;
	}
	// Update is called once per frame
	void Update () {
		
	}
}

在这里插入图片描述

使sphere成为玩家

在工具栏创建文字

将以下代码拖到白色小球上

public class PlayerController : MonoBehaviour {

	public float speed;
	private Rigidbody rb;
	public Text countText;
	public Text winText;
	private int count;

	// Use this for initialization
	void Start () {
		rb = GetComponent<Rigidbody>();
		count = 0;
		winText.text = "";
		SetCountTest();
	}
	
	void FixedUpdate() {
		float moveHorizontal = Input.GetAxis("Horizontal");
		float  moveVertical = Input.GetAxis("Vertical");
		Vector3 movement = new Vector3(moveHorizontal,0,moveVertical);
		rb.AddForce(movement * speed);
	}

	void OnTriggerEnter(Collider other) {
		if(other.gameObject.CompareTag("PickUp")){
			other.gameObject.SetActive(false);
			count++;
			SetCountTest();
		}
	}

	void SetCountTest(){
		countText.text = "Count:" + count.ToString();
		if(count >= 12){
			winText.text = "You Win";
		}
	}
	// Update is called once per frame
	void Update () {
		
	}
}

在这里插入图片描述

扫描二维码关注公众号,回复: 10067552 查看本文章
发布了3 篇原创文章 · 获赞 0 · 访问量 20

猜你喜欢

转载自blog.csdn.net/qq_43609028/article/details/105034461