Unity Rigidbody使用 之 isKinematic的使用

 时间:2024-10-11 19:40:47

Unity Rigidbody使用 之 isKinematic的使用。在Unity中,isKinematic结合Gravity使用,能使物体瞬间在有无动力效果中切换,本节介绍如何使用isKinematic实现物体重复上下运动的效果的简单案例,具体如下

工具/原料

Unity

Rigidbody

一、知识要点

1、Rigidbody:Control of an object'衡痕贤伎s position through physics simulation.Adding a Rigidbody component to an object will put its motion under the control of Unity's physics engine. Even without adding any code, a Rigidbody object will be pulled downward by gravity and will react to collisions with incoming objects if the rightCollidercomponent is also present.The Rigidbody also has a scripting API that lets you apply forces to the object and control it in a physically realistic way. For example, a car's behaviour can be specified in terms of the forces applied by the wheels. Given this information, the physics engine can handle most other aspects of the car's motion, so it will accelerate realistically and respond correctly to collisions.

2、Rigidbody.isKinematic:1)功能描述public 水瑞侮瑜boolisKinematic;潮贾篡绐Controls whether physics affects the rigidbody.If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore. The rigidbody will be under full control of animation or script control by changing transform.position. Kinematic bodies also affect the motion of other rigidbodies through collisions or joints. Eg. can connect a kinematic rigidbody to a normal rigidbody with a joint and the rigidbody will be constrained with the motion of the kinematic body. Kinematic rigidbodies are also particularly useful for making characters which are normally driven by an animation, but on certain events can be quickly turned into a ragdoll by setting isKinematic to false.2)使用案例using UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour { public Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); } void EnableRagdoll() { rb.isKinematic = false; rb.detectCollisions = true; } void DisableRagdoll() { rb.isKinematic = true; rb.detectCollisions = false; }}

二、Rigidbody使用 之 isKinematic的使用

1、打开Unity,新建一个空工程,具体如下图

Unity Rigidbody使用 之 isKinematic的使用

2、在场景中,新建“Plane”和“Cube”,并调整他们你的布局,具体如下图

Unity Rigidbody使用 之 isKinematic的使用

3、在工程中新建一个脚本“RigidbodyTest”,双击脚本或者右键“Open C# Project”打开脚本,具体如下图

Unity Rigidbody使用 之 isKinematic的使用

4、在打开的脚本“RigidbodyTest”上编辑代码,首先获取物体“Rigidbody”组件,然后在碰撞进入函数中,先把isKinematic设置为true,再重置物体位置,接着又恢复isKinematic设置为false,具体的代码和代码说明如下图

Unity Rigidbody使用 之 isKinematic的使用

5、“RigidbodyTest”脚本具体内容如下:usingUnityEngine;publicclassRigidbodyTest:Mon泠贾高框oBehaviour{privateRigidbodyrigidbody; privateVector3originalPosition; //Usethisforinitialization voidStart(){rigidbody=this.transform.GetComponent<Rigidbody>(); originalPosition=transform.position; }voidOnCollisionEnter(Collisionother){ //去动力效果,物体会瞬间停止 rigidbody.isKinematic=true; //设置物体的位置 this.transform.position=originalPosition; //回复动力效果,物体又会重力运动 rigidbody.isKinematic=false; }}

6、脚本编译正确,回到Unity,把脚本“RigidbodyTest”赋给“Cube”,并给“Cube”添加“Rigidbody”组件,具体如下图

Unity Rigidbody使用 之 isKinematic的使用

7、运行场景,即可看到物体很自然的上下运动(若去掉isKinematic的设置,效果又如何呢,大家可以试试),具体如下图

Unity Rigidbody使用 之 isKinematic的使用

8、到此,《Unity Rigidbody使用 之 isKinematic的使用》讲解结束,谢谢

  • Unity如何恢复默认的布局#校园分享#
  • 如何解决Unity脚本无法挂载的问题
  • unity3D怎么插入图片
  • U3D如何解决物体往下掉的情况
  • unity实现点击按钮,控制播放与暂停音乐
  • 热门搜索
    中秋节英语手抄报 关于诗的手抄报 安全手抄报资料 普法教育手抄报 抗战手抄报 宪法手抄报大全一等奖 2020年鼠年手抄报 珍惜生命的手抄报 邪教手抄报 数学手抄报版面设计图