当前位置:网站首页>Basic syntax of unity script (1) - common operations of game objects
Basic syntax of unity script (1) - common operations of game objects
2022-06-30 13:07:00 【ht_ game】
Common operations of game objects
Unity Many operations on game objects in use scripts to modify objects Transform( Transform attributes ) And Rigidbody( Rigid body properties ) Parameter to achieve .
The object rotates
adopt Transform
Transform.Rotate()
private void Update()
{
this.transform.Rotate(90, 0, 0);// Around the x Shaft rotation 90 degree . A positive value means clockwise , A negative value means counterclockwise
}
Pay attention to such utilization transform To realize the movement of objects , Generally, the overload with the fourth parameter is used to determine the axis of the transformation ,
Space.World: Rotate relative to the world coordinate system
Space.Self: Rotate relative to its own axis 
adopt Rigidbody
Rigidbody.MoveRotation
Format
public void MoveRotation(Quaternion rot);
Quaternion: Quaternion angle
describe : Rotate the rigid body to rotation.
Use Rigidbody.MoveRotation Rotate the rigid body , Conform to the interpolation settings of the rigid body .
If in Rigidbody It's on Rigidbody interpolation , Call Rigidbody.MoveRotation Will result in a smooth transition between the two rotations in any intermediate frame of the rendering . If you want to be in each FixedUpdate Continuous rotation of rigid bodies in , Use this option .
Rigidbody m_Rigidbody;
Vector3 m_EulerAngleVelocity;
void Start()
{
m_Rigidbody = GetComponent<Rigidbody>();
m_EulerAngleVelocity = new Vector3(0, 100, 0);
}
void FixedUpdate()
{
Quaternion deltaRotation = Quaternion.Euler(m_EulerAngleVelocity * Time.fixedDeltaTime);
m_Rigidbody.MoveRotation(m_Rigidbody.rotation * deltaRotation);
}
Objects move
The first way is through Velocity
But in most cases , The speed should not be modified directly , Because this may lead to unrealistic behavior , To set the speed of an object in each physical step .
A typical use is to change the speed when jumping in a first person shooter game , Because you want to change the speed immediately .
Rigidbody rig;
private void Update()
{
rig = this.gameObject.GetComponent<Rigidbody>();
rig.velocity = new Vector3(0, 0, 5);// Every frame y Axial movement 5 A unit of
}
The second way is through UnityAPI——Rigidbody.MovePosition Mobile location
describe : take kinematics The rigid body moves towards postion
Rigidbody.MovePosition Move the rigid body and conform to the interpolation settings . When rigid body interpolation is enabled ,Rigidbody.MovePosition Creates a smooth transition between frames .Unity Move a rigid body in each call . Occurs in world space FixedUpdate. Rigid body position Transfer from one location to another using Rigidbody.position instead of MovePosition.
public class NewBehaviourScript : MonoBehaviour // Declaration class
{
Rigidbody rig;
public float speed = 5f;
private void Start()
{
rig = this.gameObject.GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
rig.MovePosition(transform.position + input * Time.deltaTime * speed);// Move from current location to new location , Per second 5 Unit speed
}
}
The third way Rigidbody.AddForce( The most common way )
Format
public void AddForce ( Vector3 force , ForceMode mode = ForceMode.Force);
describe : Add a force to the rigid body
Force edge force The vector direction is applied continuously . Appoint ForceMode mode It is allowed to change the type of force to Acceleration、Impulse or Velocity Change.
ForceMode.Force: Interpret input as force
ForceMode.Acceleration: Interpret the parameter as acceleration
ForceMode.Impulse: Interpret the parameters as pulses
ForceMode.VelocityChange: Interpret the parameter as a direct change in velocity ( In meters / Seconds per unit ), And change the speed by the value of the force .
Forces can only be applied to active rigid bodies . If GameObject Inactive , be AddForce Invalid . Besides , Rigid bodies cannot be kinematic .
By default , Once the force is applied , The state of the rigid body is set to wake up , Unless the force is Vector3.zero.
Rigidbody rig;
public float force = 50f;
private void Start()
{
rig = this.gameObject.GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
if (Input.GetButton("Jump"))
{
rig.AddForce(transform.up * force);// Add a... Up 50 The power of the cow
}
}
adopt Transform
The first way adopt Transform.Position
private void Update()
{
transform.position = new Vector3(0, 0, 10);// towards z Axis movement 10 A unit of
}
The second way adopt UnityAPI——Transform.Translate
private void Update()
{
transform.Translate(0, 0, 5,Space.Self);// Each frame of the object is oriented to z Axial movement 5 A unit of .
}
Be careful
If a game object has a rigid body, you need to use the moving components of the rigid body .
At the same time, every frame of the rigid body calls the method to use FixedUpdate
边栏推荐
- 数据湖(十一):Iceberg表数据组织与查询
- 机器学习笔记 - 自相关和偏自相关简介
- Golang foundation -- slicing several declaration methods
- Event handling in QT
- Charles break point modify request data & response data
- Motor control Clarke( α/β) Derivation of equal amplitude transformation
- Unity脚本的基础语法(4)-访问其他游戏对象
- 全面解析免费及收费SSH工具的基本特性和总结
- The spiral matrix of the force buckle rotates together (you can understand it)
- 杭州电子商务研究院:官网(网站)是私域的唯一形态
猜你喜欢

Resource realization applet opening traffic main tutorial

Dark horse notes -- List series collections and generics

Js根据相同值将数组转换为二维数组

资源变现小程序开通流量主教程

How to use AI technology to optimize the independent station customer service system? Listen to the experts!

Shell编程概述

ABAP工具箱 V1.0(附实现思路)

How to handle ZABBIX server startup failure

杭州电子商务研究院:官网(网站)是私域的唯一形态
![[Select] resource realization information, news, we media, blog applet (can be drained, open traffic master, with PC background management)](/img/e7/1c34d8aa364b944688ec2ffb4feb7c.jpg)
[Select] resource realization information, news, we media, blog applet (can be drained, open traffic master, with PC background management)
随机推荐
Dark horse notes -- List series collections and generics
Mysql根据经纬度查询半径多少以内的数据,画个圈圈查数据库
【驚了】迅雷下載速度竟然比不上虛擬機中的下載速度
QT read / write excel--qxlsx worksheet display / hide status setting 4
All the abnormal knowledge you want is here
Solve numpy core._ exceptions. Ufunctypeerror: UFUNC 'Add' did not contain a loop with signature matching
力扣之螺旋矩阵,一起旋转起来(都能看懂)
杭州电子商务研究院:官网(网站)是私域的唯一形态
Charles break point modify request data & response data
Exploring the source code of Boca Cross Chain Communication: Elements
[one day learning awk] array usage
2022-06-23 帆软部分公式及sql生成(月份、季度取数)
Event handling in QT
Tronapi wave field interface PHP version - interface document attached - package based on thinkphp5 - source code without encryption - can be opened in two - detailed guidance of the author - 11:49:56
Qt中的事件处理
In line with the trend of media integration, Zhongke Wenge and Meishe jointly create digital intelligence media publicity
PG基础篇--逻辑结构管理(表继承、分区表)
How to handle ZABBIX server startup failure
Open source of xinzhibao applet
Resource realization applet opening wechat official small store tutorial