当前位置:网站首页>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
边栏推荐
- Clearing TinyMCE rich text cache in elementui
- Three ways for flinksql to customize udaf
- ERROR: Cannot uninstall ‘PyYAML‘. It is a distutils installed project and thus we cannot accurately
- 基于ThinkPHP5封装-tronapi-波场接口-源码无加密-可二开--附接口文档-作者详细指导-2022年6月30日08:45:27
- Tronapi- wavefield interface - source code without encryption - can be opened in two places - interface document attached - encapsulation based on thinkphp5 - detailed guidance of the author - 21:59:3
- 黑马笔记---包装类,正则表达式,Arrays类
- 商品服务-平台属性
- 黑马笔记---集合(Collection的常用方法与遍历方式)
- Discussion on JMeter operation principle
- Substrate 源码追新导读: 修复BEEFY的gossip引擎内存泄漏问题, 智能合约删除队列优化
猜你喜欢
Flinksql customizes udatf to implement topn
Idea 2021.3 golang error: rning: undefined behavior version of delve is too old for go version 1.18
jmeter 学习笔记
【C】 In depth understanding of pointers and callback functions (Introduction to simulating qsort)
Matlab tips (22) matrix analysis -- stepwise regression
Hangzhou E-Commerce Research Institute: the official website (website) is the only form of private domain
Methodology for troubleshooting problems (applicable to troubleshooting problems arising from any multi-party cooperation)
postman 自动生成 curl 代码片段
App wechat payment unicloud version of uniapp payment (with source code)
Determining the subject area of data warehouse construction
随机推荐
一次 Keepalived 高可用的事故,让我重学了一遍它!
Mqtt ROS simulates publishing a custom message type
[one day learning awk] array usage
Package tronapi wave field interface based on thinkphp5 PHP version -- interface document attached -20220627
【招聘(广州)】成功易(广州).Net Core中高级开发工程师
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
[yitianxue awk] regular matching
Wechat applet reports an error: typeerror: cannot read property 'SetData' of undefined
Questionnaire star questionnaire packet capturing analysis
【精选】资源变现资讯、新闻、自媒体、博客小程序(可引流,开通流量主,带pc后台管理)
App wechat payment unicloud version of uniapp payment (with source code)
波卡跨链通信源码探秘: 要素篇
【驚了】迅雷下載速度竟然比不上虛擬機中的下載速度
Qt中的事件处理
Kubeedge's core philosophy
[Select] resource realization information, news, we media, blog applet (can be drained, open traffic master, with PC background management)
Event handling in QT
Charles break point modify request data & response data
The independent station is Web3.0. The national "14th five year plan" requires enterprises to build digital websites!
[one day learning awk] Fundamentals