1.创建一个简单的场景。
2.制作食物并制作成预制体。给食物打上food的标签,用脚本让它被吃掉后消失。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class szsz : MonoBehaviour
{
public float zxcspeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Rotate(Vector3.up, zxcspeed);
}
}
3.创建一个可以操控的小球。并用脚本控制可以吃掉食物
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour
{
public Rigidbody rg;
// Start is called before the first frame update
void Start()
{
rg = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
rg.AddForce(new Vector3(h, 0, v));
}
//}
private void OnTriggerEnter(Collider other)//触发器的方法
{
if (other.gameObject.tag == "food")
{
Destroy(other.gameObject);
}
}
}
4.给摄像头一个可以跟随小球的脚本。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class szxc : MonoBehaviour
{
public GameObject player;
public Vector3 offset;
// Start is called before the first frame update
void Start()
{
offset = transform.position - player.transform.position;
}
// Update is called once per frame
void Update()
{
transform.position = player.transform.position + offset;
}
}
5.这样就可以玩了