所有脚本
相机跟随
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowTarget : MonoBehaviour
{
public Transform PlayerTransform;
private Vector3 offset;//因为不需要通过外界设置值,所以用private
// Start is called before the first frame update
void Start()
{
//先定义两个位置的差 引用Player上的组件,要先在最开始定义
offset = transform.position - PlayerTransform.position;
//定义用Vector3 offset ,但是在这个方法里定义的值无法被其他方法引用
}
// Update is called once per frame
void Update()
{
transform.position = PlayerTransform.position+offset;//不能直接引用到offsrt,因此把offset转化成属性
}
}
物品旋转
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Food : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//旋转的实现方法,用Rotate,设置旋转角度为上
transform.Rotate(Vector3.up);
}
}
玩家操作
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
public GameObject Wintext;
public Text scoreText;
public int score = 0;//定义一个分数,整数变量
public Rigidbody rd;
// Start is called before the first frame update
void Start()
{
//Debug.Log("游戏开始了");
rd = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
//rd.AddForce(new Vector3(10, 0, 0));
float h = Input.GetAxis("Horizontal");//-1 1 ad
float v = Input.GetAxis("Vertical");//ws
//Debug.Log(h);
rd.AddForce(new Vector3(h, 0, v));
}
//使用一个系统事件写碰撞方法 碰撞有三个状态 这里只需要用到enter collision保存了碰撞信息
private void OnCollisionEnter(Collision collision)
{
//collision.collider.tag;无论是通过游戏物体还是通过碰撞组件,都可以获得物体的标签
if (collision.gameObject.tag == "Food")//判断碰撞体是否是Food
{
Destroy(collision.gameObject);
}
//Debug.Log("发生了碰撞 OnCollisionEnter");
}
//private void OnCollisionExit(Collision collision)
//{
// //Debug.Log("发生了碰撞 OnCollisionExit");
//}
//private void OnCollisionStay(Collision collision)
//{
// //Debug.Log("发生了碰撞 OnCollisionStay");
//}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Food")//判断碰撞体是否是Food
{
Destroy(other.gameObject);
score++;
scoreText.text = "分数:" + score;
if (score == 8)
{
Wintext.SetActive(true);
}
}
}
}