1 导入fbx模型
点击左下角的asset,右键选择import new asset,对于fbx文件可以使用3dmax直接对模型进行导出的。如果没有的话,可以找我哦。fbx文件名称最好不要含有中文,我这里的名称为2.fbx。
2 绑定代码
新建代码,代码名称就是你的类名,如果代码名称修改了一定要修改类名,否则会出现编译错误的。
我这里使用的是默认的文件名称: NewBehaviourScript。
public float speed = 3.0f;
public float sensitivityX = 10.0f;
public float sensitivityY = 10.0f;
//上下最大视角(Y视角)
public float minmumY = -60f;
public float maxmunY = 60f;
float rotationY = 0f;
// Start is called before the first frame update
void Start()
{
print("hello world\n");
Rigidbody rigidbody = GetComponent<Rigidbody>();
if (rigidbody)
{
rigidbody.freezeRotation = true;
}
}
添加相关的函数变量和函数实现方法:
public float speed = 3.0f;
public float sensitivityX = 10.0f;
public float sensitivityY = 10.0f;
//上下最大视角(Y视角)
public float minmumY = -60f;
public float maxmunY = 60f;
float rotationY = 0f;
// Start is called before the first frame update
void Start()
{
print("hello world\n");
Rigidbody rigidbody = GetComponent<Rigidbody>();
if (rigidbody)
{
rigidbody.freezeRotation = true;
}
}
编辑搜图
请点击输入图片描述
// Update is called once per frame
void Update()
{
this.transform.Rotate(Vector3.up * speed);
GameObject model_fbx = GameObject.Find("2");
if (!GameObject.Find("2"))//你要找的模型名称是你导入的fbx的文件的名称,调用这个函数即可
{
print("no model_fbx exits\n");
}
model_fbx.transform.Rotate(Vector3.up * speed);//调用这个即可进行模型的旋转
//根据鼠标移动的快慢(增量),获得相机左右旋转的角度(处理X)
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
//根据鼠标移动的快慢(增量),获取相机上下移动的角度(处理Y)
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
//角度限制,rotationY小于min返回min 大于max 返回max 否则返回value
rotationY = Clamp(rotationY, maxmunY, minmumY);
//设置摄像机角度
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
public float Clamp(float value, float max, float min)
{
if (value < min) return min;
if (value > max) return max;
return value;
}
// Update is called once per frame
void Update()
{
this.transform.Rotate(Vector3.up * speed);
GameObject model_fbx = GameObject.Find("2");
if (!GameObject.Find("2"))//你要找的模型名称是你导入的fbx的文件的名称,调用这个函数即可
{
print("no model_fbx exits\n");
}
model_fbx.transform.Rotate(Vector3.up * speed);//调用这个即可进行模型的旋转
//根据鼠标移动的快慢(增量),获得相机左右旋转的角度(处理X)
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
//根据鼠标移动的快慢(增量),获取相机上下移动的角度(处理Y)
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
//角度限制,rotationY小于min返回min 大于max 返回max 否则返回value
rotationY = Clamp(rotationY, maxmunY, minmumY);
//设置摄像机角度
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
public float Clamp(float value, float max, float min)
{
if (value < min) return min;
if (value > max) return max;
return value;
}
将代码拖入到你新建的fbx模型上即可,然后点击运行进行效果的查看即可。
3 运行效果
编辑搜图
请点击输入图片描述
欢迎跟我一起学开发。