这一部分内容比较基础,主要是给自己看的,各位高手手下留情,请自动忽略
1、预制体Prefab,可以在游戏中重复使用。
制作预制体,将场景中的物体拖动到Asset下自动创建
2、摄像机:
选中GameObject->Align with view,当前视角和位置
相关属性:
todo:脚本控制摄像机的移动和旋转,视野FOV等。
3、组件Component:习惯u3d基于组件的开发方式
3.1 移动组件Transform和键盘交互:
//键盘交互
if(Input.GetKey(KeyCode.A))
{
Debug.Log("moving");
}
if (Input.GetKeyDown(KeyCode.W))
{
Debug.Log("action1");
}
if (Input.GetKeyUp(KeyCode.K))
{
Debug.Log("action2");
}
//鼠标输入 0-> 左键 1-> 右键 2->中键
if (Input.GetMouseButton(0))
{
Debug.Log("左键按住");
}
if (Input.GetMouseButtonDown(2))
{
Debug.Log("中键按下");
}
if (Input.GetMouseButtonUp(1))
{
Debug.Log("右键抬起");
}
// Start is called before the first frame update
private Transform FT1;
void Start()
{
FT1 = gameObject.GetComponent<Transform>();
}
//利用变换组件来移动位置
//
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.W))
{
FT1.Translate(Vector3.forward *0.1f,Space.Self);
}
if (Input.GetKey(KeyCode.A))
{
FT1.Translate(Vector3.left*0.1f,Space.Self);
}
if (Input.GetKey(KeyCode.S))
{
FT1.Translate(Vector3.back*0.1f,Space.Self);
}
if (Input.GetKey(KeyCode.D))
{
FT1.Translate(Vector3.right*0.1f,Space.Self);
}
}
3.2 刚体组件:Rigidbody
添加方式:Component->Phsics->Rigidbody
注:刚体组件的移动方式是基于世界坐标系的,会触发物理相关的事件
private Rigidbody m_Rigidbody;
private Transform m_Transform;
// Start is called before the first frame update
void Start()
{
m_Rigidbody = gameObject.GetComponent<Rigidbody>();
m_Transform = gameObject.GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.W))
{
m_Rigidbody.MovePosition(m_Transform.position + Vector3.forward*0.1f);
// m_Transform.Translate(Vector3.forward *0.1f,Space.Self);
}
if (Input.GetKey(KeyCode.A))
{
m_Rigidbody.MovePosition(m_Transform.position + Vector3.left*0.1f);
// m_Transform.Translate(Vector3.left*0.1f,Space.Self);
}
if (Input.GetKey(KeyCode.S))
{
m_Rigidbody.MovePosition(m_Transform.position + Vector3.back*0.1f);
//m_Transform.Translate(Vector3.back*0.1f,Space.Self);
}
if (Input.GetKey(KeyCode.D))
{
m_Rigidbody.MovePosition(m_Transform.position + Vector3.right*0.1f);
//m_Transform.Translate(Vector3.right*0.1f,Space.Self);
}
3.3 碰撞体:
component- >Physics -> collider
其中mesh collider可以根据选定的网格来生成碰撞体。
合理设置rigidbody和collider