using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
public Rigidbody rd;
public int score=0;
public Text scoreText;
public GameObject winText;
// Start is called before the first frame update
void Start()
{
rd = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
rd.AddForce(new Vector3(h, 0, v));
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Food")
{
Destroy(other.gameObject);
score++;
scoreText.text = "分数:" + score;
if (score == 14)
winText.SetActive(true);
}
}
}
CameraMove类(摄像机跟随)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove : MonoBehaviour
{
// Start is called before the first frame update
public Transform playerLocation;
private Vector3 offset;
void Start()
{
offset = transform.position - playerLocation.position;
}
// Update is called once per frame
void Update()
{
transform.position = playerLocation.position + offset;
}
}
StarRotate类(控制得分物体旋转)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StarRotate : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
this.transform.Rotate(1, 0, 0);
}
}