多语言展示
当前在线:1172今日阅读:84今日分享:32

Unity Rigidbody使用 之 isKinematic的使用

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

Unity

2

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 right Collider component 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 bool isKinematic;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();    }    void EnableRagdoll() {        rb.isKinematic = false;        rb.detectCollisions = true;    }    void DisableRagdoll() {        rb.isKinematic = true;        rb.detectCollisions = false;    } }

二、Rigidbody使用 之 isKinematic的使用
1

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

2

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

3

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

4

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

5

“RigidbodyTest”脚本具体内容如下:using UnityEngine;public class RigidbodyTest : MonoBehaviour {    private Rigidbody rigidbody;    private Vector3 originalPosition;    // Use this for initialization    void Start () {        rigidbody = this.transform.GetComponent  ();        originalPosition = transform.position;    }    void OnCollisionEnter(Collision other){                //去动力效果,物体会瞬间停止        rigidbody.isKinematic = true;        //设置物体的位置        this.transform.position = originalPosition;        //回复动力效果,物体又会重力运动        rigidbody.isKinematic = false;    }}

6

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

7

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

8

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

注意事项

您的支持,是我们不断坚持知识分享的动力,若帮到您,还请帮忙投票有得;若有疑问,请留言

推荐信息