当前位置:网站首页>Unity learning notes - six common functions of object movement
Unity learning notes - six common functions of object movement
2022-07-27 19:04:00 【Miracle Fan】
List of articles
1.Transform.Translate
Use this method to modify the position , Will ignore other objects , direct penetration .
1.1 Syntax
//1. Using three-dimensional coordinates Translation For mobile
Translate(Vector3.translation,[Space.relativeTo =Space.self]);
//2. utilize x,y,z Coordinate transformation of axis
Translate(float x,float y,float z,[Space.relativeTo =Space.self]);
//3. Move relative to the specified object
Translate(Vector3 translation,[Transform relativeTo]);
Translate(float x,float y,float z,[Transform relativeTo])
//[] Internal parameters are optional parameters , The default is the world coordinate system
1.2 Example
void Update()
{ gameObject.transform.Translate(Vector3.forward);
}

2.Vectoe3.Lerp
The linear change is relatively smooth , In line with the actual situation
2.1 Syntax
Vector3.Lerp(Vector3 a,Vector3 b,float c)
| Parameters | explain |
|---|---|
| a | The starting position |
| b | Destination location |
| t | stay a~b Percentage of movement between |
2.2 Example
public Vector3 targetpositon = new Vector3(5,4,10);
void Update()
{
var t =1/(transform.position - targetpositon).magnitude;
gameObject.transform.position = Vector3.Lerp(transform.position, targetpositon, t*0.01f);
}

3. Vector3.SmoothDamp
3.1 Syntax
Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed = Mathf.Infinity, float deltaTime = Time.deltaTime);
| Parameters | explain |
|---|---|
| current | The present position |
| target | Destination location |
| currentVelocity | Current speed , Each call will modify its value |
| smoothTime | The ideal time to reach the target location |
| maxSpeed | Maximum allowable speed (optional) |
| deltaTime | The difference between the previous frame rate and the next frame rate |

4.Rigidbody
By adding rigid body components to objects , Modify the velocity value of the rigid body
4.1Example
private Rigidbody rb;
void Start()
{
// Get the rigid body component
rb= GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
rb.velocity =new Vector3(1,0,0);// Just set up x The directional velocity is 1
}

5.AddForce
5.1 Syntax
AddForce(Vector3 force, ForceMode mode = ForceMode.Force);
AddForce(float x, float y, float z, ForceMode mode = ForceMode.Force);
5.2 Example
private Rigidbody rb;
void Start()
{
rb= GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// Each time you press the space bar, you add x Directional force
if(Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(new Vector3(10, 0, 0));
}
}

6. MoveTowards
6.1 Syntax
Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta);
// The current position , Target location , The distance of each move .
6.2 Example
public float speed;
public Vector3 targetposition= Vector3.zero;// Set the target location
// Update is called once per frame
void Update()
{
float step = speed * Time.deltaTime; // Calculate the distance
gameObject.transform.position = Vector3.MoveTowards(transform.position, targetposition, step);
}

边栏推荐
- WinForm screenshot save C code
- normal distribution, lognormal distribution,正态随机数的生成
- 转行软测&跳槽到新公司,工作怎样快速上手?
- Hash、Set、List、Zset、BitMap、Scan
- [NPM] the "NPM" item cannot be recognized as the name of cmdlets, functions, script files or runnable programs. Please check the spelling of the name. If the path is included, make sure the path is co
- MySQL 06 transaction, view, index, backup and recovery
- 连续时间系统的性能分析(2)-二阶系统性能改善方式PID,PR
- Unity学习笔记(刚体-物理-碰撞器-触发器)
- SQL server stored procedures multi angle introduction suggestions collection
- 用函数在Excel中从文本字符串提取数字
猜你喜欢

自控原理学习笔记-系统稳定性分析(1)-BIBO稳定及Routh判据

Big enemies, how to correctly choose the intended enterprises in the new testing industry?

Matplotlib (basic usage)

连续时间系统的性能分析(1)-控制系统性能指标及一二阶分析

Redis注解

MySQL 03 高级查询(一)
![[wechat applet] project practice - lottery application](/img/08/1e8643c95ad7c2661a76f9c3a0c57d.png)
[wechat applet] project practice - lottery application
![[NPM] the](/img/ae/efccefae0323a1f6a425523e01d2ac.png)
[NPM] the "NPM" item cannot be recognized as the name of cmdlets, functions, script files or runnable programs. Please check the spelling of the name. If the path is included, make sure the path is co

Order submission

Overview of Baidu map technology, and application development of basic API and webapi
随机推荐
MySQL 04 advanced query (II)
I'm stupid. When completable future is used with openfegin, it even reports an error
连接查询和子查询
SSM integration
Product name fuzzy search:
JDBC MySQL 01 JDBC operation MySQL (add, delete, modify and query)
Commodity comment information and comment information classification
用函数在Excel中从文本字符串提取数字
订单的提交
[NPM] the "NPM" item cannot be recognized as the name of cmdlets, functions, script files or runnable programs. Please check the spelling of the name. If the path is included, make sure the path is co
Redis annotation
MySQL 02 initial experience
MySQL 05 stored procedure
JS to realize simple form verification and select all functions
【微信小程序】项目实战—抽签应用
Kinect for Unity3D——BackgroundRemovalDemo学习
Extension of regular expression
npm 基本使用
MySQL 02 初体验
SQL server stored procedures multi angle introduction suggestions collection