首页 技术 正文
技术 2022年11月7日
0 收藏 556 点赞 322 浏览 3148 个字

1. 简介

在Unity3D中,有多种方式可以改变物体的坐标,实现移动的目的,其本质是每帧修改物体的position。之前写过类似的文章,这次增加了平时常用API的脚本,每个脚本均手打测试可用.

2. 通过Transform组件移动物体

Transform组件用于描述物体在空间中的状态,它包括位置(position)旋转(rotation)缩放(scale)。 其实所有的移动都会导致position的改变,这里所说的通过Transform组件来移动物体,指的是直接操作Transform来控制物体的位置(position)

2.1 Transform.Translate

该方法可以将物体从当前位置,移动到指定位置,并且可以选择参照的坐标系。 当需要进行坐标系转换时,可以考虑使用该方法以省去转换坐标系的步骤。

public float m_speed = 5f;
//Translate移动控制函数
void MoveControlByTranslate()
{
if (Input.GetKey(KeyCode.W)|Input.GetKey(KeyCode.UpArrow)) //前
{
this.transform.Translate(Vector3.forward*m_speed*Time.deltaTime);
}
if (Input.GetKey(KeyCode.S) | Input.GetKey(KeyCode.DownArrow)) //后
{
this.transform.Translate(Vector3.forward *- m_speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A) | Input.GetKey(KeyCode.LeftArrow)) //左
{
this.transform.Translate(Vector3.right *-m_speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D) | Input.GetKey(KeyCode.RightArrow)) //右
{
this.transform.Translate(Vector3.right * m_speed * Time.deltaTime);
}
}

或者

 //Translate移动控制函数
void MoveControlByTranslateGetAxis()
{
float horizontal = Input.GetAxis("Horizontal"); //A D 左右
float vertical = Input.GetAxis("Vertical"); //W S 上 下 transform.Translate(Vector3.forward * vertical * m_speed * Time.deltaTime);//W S 上 下
transform.Translate(Vector3.right * horizontal * m_speed * Time.deltaTime);//A D 左右
}

2.2 Vector3.Lerp, Vector3.Slerp, Vector3.MoveTowards

Vector3既可以表示三维空间中的一个点,也可以表示一个向量。这三个方法均为插值方法,Lerp为线性插值,Slerp为球形插值,MoveTowards在Lerp的基础上增加了限制最大速度功能。 当需要从指定A点移动到B点时,可以考虑时候这些方法。

2.3 Vector3.SmoothDamp

该方法是可以平滑的从A逐渐移动到B点,并且可以控制速度,最常见的用法是相机跟随目标。

2.4 Transform.position

有时重新赋值position能更快实现我们的目标。

3. 通过Rigidbody组件移动物体

Rigidbody组件用于模拟物体的物理状态,比如物体受重力影响,物体被碰撞后的击飞等等。

注意:关于Rigidbody的调用均应放在FixedUpdate方法中,该方法会在每一次执行物理模拟前被调用。

3.1 Rigidbody.velocity

设置刚体速度可以让物体运动并且忽略静摩擦力,这会让物体快速从静止状态进入运动状态。

  //Velocity移动控制函数
void MoveControlByVelocity()
{
float horizontal = Input.GetAxis("Horizontal"); //A D 左右
float vertical = Input.GetAxis("Vertical"); //W S 上 下
//这个必须分开判断 因为一个物体的速度只有一个
if (Input.GetKey(KeyCode.W) | Input.GetKey(KeyCode.S))
{
m_rigidbody.velocity = Vector3.forward * vertical * m_speed;
}
if (Input.GetKey(KeyCode.A)|Input.GetKey(KeyCode.D))
{
m_rigidbody.velocity = Vector3.right * horizontal * m_speed;
}
}

3.2 Rigidbody.AddForce

给刚体添加一个方向的力,这种方式适合模拟物体在外力的作用下的运动状态。

//AddForce移动控制函数
void MoveControlByAddForce()
{
float horizontal = Input.GetAxis("Horizontal"); //A D 左右
float vertical = Input.GetAxis("Vertical"); //W S 上 下 m_rigidbody.AddForce(Vector3.forward * vertical * m_speed);
m_rigidbody.AddForce(Vector3.right * horizontal * m_speed); }

3.3 Rigidbody.MovePosition

刚体受到物理约束的情况下,移动到指定点。

4. 通过CharacterController组件移动物体

CharacterController用于控制第一人称或第三人称角色的运动,使用这种方式可以模拟人的一些行为,比如限制角色爬坡的最大斜度,步伐的高度等。

4.1 CharacterController.SimpleMove

用于模拟简单运动,并且自动应用重力,返回值表示角色当前是否着地。

    //SimpleMove移动控制函数 角色控制器
void MoveControlBySimpleMove()
{
float horizontal = Input.GetAxis("Horizontal"); //A D 左右
float vertical = Input.GetAxis("Vertical"); //W S 上 下 m_character.SimpleMove(transform.forward * vertical * m_speed);
}

4.2 CharacterController.Move

模拟更复杂的运动,重力需要通过代码实现,返回值表示角色与周围的碰撞信息。

//Move移动控制函数 角色控制器
void MoveControlByMove()
{
float horizontal = Input.GetAxis("Horizontal"); //A D 左右
float vertical = Input.GetAxis("Vertical"); //W S 上 下
float moveY = 0; m_gravity=10f;
moveY-= m_gravity * Time.deltaTime;//重力 m_character.Move(new Vector3(horizontal, moveY, vertical) * m_speed * Time.deltaTime);
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,914
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,439
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,252
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,064
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,698
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,737