unity中物理组件的添加_Unity Engine物理组件

本文介绍了Unity中的2D物理基础,包括如何向游戏对象添加物理属性,理解向量和力的概念,以及刚体、碰撞器和碰撞在2D游戏中的应用。通过学习,你将掌握如何在Unity中创建具有真实物理行为的游戏元素。
摘要由CSDN通过智能技术生成

unity中物理组件的添加

One of the core foundations of most games, whether they be 2D or 3D, is the Physics involved in them. Physics need not be complicated stuff like wave equations and fluid dynamics. Even the most basic stuff that you do in making a game involves physics.

不管是2D还是3D,大多数游戏的核心基础之一就是物理原理。 物理学不需要像波动方程和流体动力学这样的复杂事物。 甚至您制作游戏中最基本的内容都涉及物理。

For example, moving the player generally involves either adding a force on the object that is the player, or displacing it by a certain amount. Making it float or drown in water, or just slide down slopes is also involved with how physics work.

例如,移动播放器通常涉及在作为播放器的对象上施加力,或将其移动一定量。 使它漂浮或淹没在水中,或者只是滑下斜坡也与物理学的工作方式有关。

Unity Engine Physics Components
Unity Engine Physics Components

Physics in Unity, and in general game design, are really important. Most of the time, it's the physics of a game which cause "bugs" or "glitches". Some examples of these would be OoBs (Out of Bounds) and clipping errors.

Unity中的物理以及一般游戏设计中的物理非常重要。 在大多数情况下,游戏的物理原因会导致"bugs""glitches" 。 其中一些示例是OoB(越界)和裁剪错误。

In this section, we'll be looking at how we can add physical properties to objects in our game, and how we can make them work the way we want them too. We'll also learn more about referencing objects and the magic of public.

在本节中,我们将研究如何向游戏中的对象添加物理属性,以及如何使它们也按我们希望的方式工作。 我们还将学习有关引用对象和公共魔术的更多信息。

Unity:从2D物理开始 (Unity: Starting with 2D Physics)

Unity Engine Physics Components

As the name implies, 2D means working in two dimensions. As such, we don't really care about what's going on in the third dimension (or the Z-axis.) As far as we're concerned, we only need to care about the X axis (what's going on horizontally) and the Y axis (what's going on vertically).

顾名思义,二维意味着在二维中工作。 因此,我们实际上并不关心第三维(或Z轴)上发生的事情。就我们而言,我们只需要关心X轴(水平上进行的事情)和Y轴(垂直方向)。

In fact, here's a little thing you can try out before we get started. In Unity, try clicking on the 2D button on the top-left of the scene view.

实际上,您可以在开始之前尝试一下这一点。 在Unity中,尝试单击场景视图左上角的2D按钮。

Unity Engine 2D Physics Components

What you'll see is something like this:

您将看到的是这样的:

Unity Engine 2D Physics Components

You'll notice that Unity is actually just making us think that we're working in 2D when in reality we're actually just making a flat 3D game! We can actually use this trick that Unity uses to our own advantage, as we'll see later on when we discover Lighting effects.

您会注意到,Unity实际上只是让我们认为我们正在2D工作,而实际上我们实际上只是在制作平面3D游戏! 实际上,我们可以利用Unity使用的这一技巧来发挥自己的优势,正如我们稍后将在发现Lighting效果时看到的那样。

Alright, time to get started. Here's what we'll be working with:

好了,该开始了。 这是我们将要处理的内容:

  • Vectors

    向量

  • Rigid-bodies and Forces

    刚体和力量

  • Colliders and Collisions

    撞机和碰撞

  • Your first game

    你的第一场比赛

We will explain what Unity vectors and forces are here, and from the next lesson onwards, we will start off with Rigidbodies and Collisions.

我们将在这里解释什么是Unity向量和力,从下一课开始,我们将从RigidbodiesCollisions开始

In Unity, we have two scripting classes, named Vector2 and Vector3(Actually, there's a Vector4 as well, but we're not really concerned with that for the time being). Vector2 and Vector3 are what you can call containers for a number of numeric values, most often floating point values, represented by the number on the end. So, Vector2 is a container for two individual values, and Vector3 is a container for 3 values. When we work in Unity2D, the Vector2 class becomes prominent, although Vector3 still has some uses.

在Unity中,我们有两个脚本类,分别是Vector2Vector3 (实际上,也有Vector4,但是暂时我们并不在意)。 Vector2Vector3是您可以调用的containers用于包含许多数字值,通常是浮点值,由末尾的数字表示。 因此, Vector2是两个单独值的容器,而Vector3是三个值的容器。 当我们在Unity2D中工作时,尽管Vector3仍然有一些用途,但Vector2类变得很重要。

Since this might be a bit confusing for some readers, let's understand with an example. Let's say we have the position of an object, say Mr. Star. His position in the 2D game world is defined by an X value (that is, horizontally) and a Y value (vertically).

因为这可能会使一些读者感到困惑,所以让我们看一个例子。 斯塔先生说,假设我们有一个物体的位置。 他在2D游戏世界中的位置由X值(即水平)和Y值(垂直)定义。

In writing code for Unity, instead of making two separate values for the position of Mr. Star, we use a Vector2 which holds two variables instead. These variables can either be defined, or can be set to other user-defined variables.

在为Unity编写代码时,我们没有使用两个保存两个变量的Vector2来代替Star先生的位置设置两个单独的值。 这些变量可以定义,也可以设置为其他用户定义的变量。

Unity Engine 2D Physics Components

NOTE: Don't worry about where to write your C# code, we will show you how to add your own scripts soon.

注意:不用担心在哪里编写C#代码,我们将向您展示如何尽快添加自己的脚本。

Of course, a Vector2 isn't really limited to defining a position. A Vector has its best use whenever you're dealing with values that modify (or define) the location, rotation or direction of something.

当然, Vector2并不仅限于定义位置。 每当您处理修改(或定义)某物的位置,旋转或方向的值时,Vector便会发挥其最佳作用。

Note: If you didn't install Visual Studio when you installed Unity, your scripts will most likely open in Unity's default interface, MonoDevelop. Don't worry if code looks different than yours in color or something like that; as long as you're following what's going on, you'll be fine.

注意:如果在安装Unity时未安装Visual Studio,则脚本很可能会在Unity的默认界面MonoDevelop中打开。 如果代码看起来与您的颜色或类似内容有所不同,请不要担心; 只要您跟踪发生的事情,就可以了。

翻译自: https://www.studytonight.com/game-development-in-2D/2D-physics-components

unity中物理组件的添加

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值