Unity:使用RigidBody

There are mainly two ways of moving a gameObject in Unity:

在Unity中移动游戏对象的方式主要有两种:

  • Changing Position Coordintes: By directly changing the position of a gameObject without much consideration to its physics or other such components. This is what we've been doing so far, by simply adding a value to the object's X position every frame.

    更改位置坐标:通过直接更改游戏对象的位置而无需过多考虑其物理性质或其他类似组件。 这是到目前为止我们一直在做的事情,只需在每个帧的对象的X位置添加一个值即可。

  • Rigidbody Physics: When dealing with objects that follows physics rules, it makes more sense to apply forces on objects or change their velocity instead of a direct position update. Looks more real.

    刚体物理:处理遵循物理规则的对象时,将力施加到对象上或更改其速度而不是直接更新位置更有意义。 看起来更真实。

In this tutorial we will cover an interesting example of using Rigidbody Physics for movement instead of Position change trick. We've worked with shooting bullets so far, right? But our bullets have only travelled using the position change regardless of what they are and how they move in real world.

在本教程中,我们将介绍一个有趣的示例,该示例使用刚体物理进行运动而不是位置更改技巧。 到目前为止,我们已经进行了子弹射击工作,对吗? 但是,无论子弹是什么,以及它们在现实世界中的移动方式如何,我们的子弹只能利用位置变化来移动。

Which also leads to issues like the one in which our bullet kept going even after it hit the target(Checkout our previous tutorials for the example).

这还会导致类似这样的问题:即使子弹击中目标,我们的子弹仍会继续运转(请查看示例的先前教程)。

It simply didn't know that it was supposed to stop, after hitting the target, since we programmed it to keep going to the right.

它根本不知道在击中目标后应该停止,因为我们对它进行了编程以使其继续向右移动。

Instead of doing that, what if we applied a very strong impulsive force to the bullet, much like the one in real life? Doing so will make the bullet move because of its momentum, and not because of a programmed position update.

而不是这样做,如果我们对子弹施加非常强的脉冲力 ,就像现实生活中的那种那样,该怎么办? 这样做会使子弹运动,因为它具有动力,而不是因为程序化了的位置更新。

Let's explore this option, and, we'll understand the AddForce() method provided by the class Rigidbody. Open up the script that defines our bullet/fireball's behaviour:

让我们探索这个选项,然后,我们将理解Rigidbody类提供的AddForce()方法。 打开定义我们的子弹/火球行为的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FireballBehaviour : MonoBehaviour
{
    private
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值