https://www.bilibili.com/video/BV18U4y187ug
asset/.meta 属性文件
preference→external tolls 更改编辑器
asset 右键菜单 creat
center / pivot 原点坐标
GameObject → Component Render → Material
游戏引擎:高复用模块的整合
文件名与类名保持一致
Start 开始方法
Update 更新方法
prefabs 预先制作好的物体
instance 实例(对象)
物体名字&标签
触发检测 & 碰撞检测
勾选 Box Collider 的 Is TriggerP
//Player.cs
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()
{
// Debug.Log("game start");
rd = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// Debug.Log("game playing");
// Vector3.right left back forward
// (x,y,z) (2,0,0)
// rd.AddForce(Vector3.right);
// rd.AddForce( new Vector3(10,0,0) );
float h = Input.GetAxis("Horizontal"); //-1 1
float v = Input.GetAxis("Vertical");
Debug.Log(h);
rd.AddForce( new Vector3(h,0,v) );
}
//触发检测
private void OnTriggerEnter(Collider other)
{
Debug.Log("OnTriggerEnter"+other.tag);
if (other.tag == "Food")
{
Destroy(other.gameObject);
score++;
scoreText.text = "Socre:" + score;
if(score == 5)
{
winText.SetActive(true);
}
}
}
private void OnTriggerExit(Collider other)
{
}
private void OnTriggerStay(Collider other)
{
}
/*
//碰撞检测的方式
private void OnCollisionEnter(Collision collision)
{
//获取碰撞物体上的标签
//collision.collider.tag
if (collision.gameObject.tag == "Food")
{
Destroy(collision.gameObject);
}
}
private void OnCollisionExit(Collision collision)
{
}
private void OnCollisionStay(Collision collision)
{
}
*/
}
//FollowTarget.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowTarget : MonoBehaviour
{
public Transform playerTransform;
private Vector3 offset;
//通过拖拽方式获得小球位置
// Start is called before the first frame update
void Start()
{
//计算位置差
offset = transform.position - playerTransform.position;
}
// Update is called once per frame
void Update()
{
transform.position = playerTransform.position + offset;
}
}
//Food.cs
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()
{
transform.Rotate( Vector3.up );
}
}