unity3d 高度夹角设置

2015年01月20日 10:39 0 点赞 0 评论 更新于 2025-11-21 14:54

物体移动

指定方向移动

在 Unity3D 中,若要使物体沿指定方向移动,可按以下方式操作。首先,定义移动速度,然后使用 transform.Translate 方法实现移动。示例代码如下:

// 移动速度
float TranslateSpeed = 10f;
// Vector3.forward 表示“向前”
transform.Translate(Vector3.forward * TranslateSpeed);

上述代码中,TranslateSpeed 为移动速度,Vector3.forward 代表向前的方向,通过 transform.Translate 方法让物体向前移动。

全方向移动

若要实现物体在全方向上的移动,需要分别设置不同轴的移动速度。示例代码如下:

// x 轴移动速度
float xSpeed = -5f;
// z 轴移动速度
float zSpeed = 10f;
// 向 x 轴移动 xSpeed,同时向 z 轴移动 zSpeed,y 轴不动
transform.Translate(xSpeed, 0, zSpeed);

这里分别定义了 xSpeedzSpeed 作为 x 轴和 z 轴的移动速度,通过 transform.Translate 方法使物体在 x 轴和 z 轴方向上移动。

重置坐标

若要将物体直接移动到指定的三维空间位置,可直接设置 transform.position。示例代码如下:

// x 轴坐标
float xPostion = -5f;
// z 轴坐标
float zPostion = 10f;
// 直接将当前物体移动到 x 轴为 xPostion,y 轴为 0,z 轴为 zPostion 的三维空间位置。
transform.position = new Vector3(xPostion, 0, zPostion);

通过设置 transform.position 为一个新的 Vector3 对象,将物体移动到指定位置。

输入控制

输入指定按键

在 Unity3D 中,可以通过检测键盘按键的输入来执行相应操作。示例代码如下:

// 按下键盘“上方向键”
if (Input.GetKey("up"))
print("Up!");
// 按下键盘“W 键”
if (Input.GetKey(KeyCode.W))
print("W!");

上述代码中,使用 Input.GetKey 方法检测按键输入,当按下“上方向键”或“W 键”时,会在控制台输出相应信息。

鼠标控制

鼠标控制主要包括检测鼠标按键的按下以及获取鼠标的移动增量。示例代码如下:

// 按下鼠标左键(0 对应左键 , 1 对应右键 , 2 对应中键)
if (Input.GetMouseButton(0))
print("Mouse Down!");
// 鼠标横向增量(横向移动)
float mouseX = Input.GetAxis("Mouse X");
// 鼠标纵向增量(纵向移动)
float mouseY = Input.GetAxis("Mouse Y");

使用 Input.GetMouseButton 方法检测鼠标按键的按下,使用 Input.GetAxis 方法获取鼠标的横向和纵向移动增量。

获取轴

可以通过 Input.GetAxis 方法获取水平轴和垂直轴的输入值,该值在控制器和键盘输入时范围在 -1 到 1 之间。示例代码如下:

// 水平轴
float horizontal = Input.GetAxis("Horizontal");
// 垂直轴
float vertical = Input.GetAxis("Vertical");

物体旋转

按住鼠标拖动物体旋转

通过鼠标左键的按下和鼠标的移动来实现物体的旋转。示例代码如下:

float speed = 100.0f;
float x;
float y;

void Update()
{
if (Input.GetMouseButton(0)) // 鼠标按着左键移动
{
y = Input.GetAxis("Mouse X") * Time.deltaTime * speed;
x = Input.GetAxis("Mouse Y") * Time.deltaTime * speed;
}
else
{
x = y = 0;
}
// 旋转角度(增加)
transform.Rotate(new Vector3(x, y, 0));
}

Update 方法中,检测鼠标左键是否按下,若按下则根据鼠标的移动增量计算旋转角度,然后使用 transform.Rotate 方法旋转物体。

平滑旋转至自定义角度

若要将物体平滑旋转至自定义角度,可按以下步骤操作:

bool iszhuan = false;
Quaternion targetRotation;

void OnGUI()
{
if (GUI.Button(new Rect(Screen.width - 110, 10, 100, 50), "set Rotation"))
{
// 自定义角度
targetRotation = Quaternion.Euler(45.0f, 45.0f, 45.0f);
// 平滑旋转至目标角度
iszhuan = true;
}
}

void pinghuaxuanzhuan()
{
if (iszhuan)
{
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 3);
}
}

void Update()
{
pinghuaxuanzhuan();
}

OnGUI 方法中,创建一个按钮,当点击按钮时,设置自定义的旋转角度,并将 iszhuan 标志置为 true。在 pinghuaxuanzhuan 方法中,使用 Quaternion.Slerp 方法实现平滑旋转。

键盘控制物体缩放

通过键盘的输入来控制物体的缩放。示例代码如下:

float speed = 5.0f;
float x;
float z;

void Update()
{
x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;    // 水平
z = Input.GetAxis("Vertical") * Time.deltaTime * speed;      // 垂直
transform.localScale += new Vector3(x, 0, z);
}

Update 方法中,根据键盘的水平和垂直输入计算缩放增量,然后更新物体的 localScale

以上就是 Unity3D 中关于物体移动、输入控制、旋转和缩放的相关操作代码及说明。