[英]How do you push a game object with another game object a set distance e.g. by 1 tile
I'm trying to make a 2d game and have a grid in Unity in 2D mode, TOP DOWN fashion and i have a player and a box.我正在尝试制作一个 2d 游戏,并在 Unity 中以 2D 模式、TOP DOWN 方式创建一个网格,并且我有一个播放器和一个盒子。 I need the player to move one tile per key press and when it collides with the box sprite, i need to "push" the sprite one tile in the pushing direction for all 4 sides of the box.
我需要玩家每次按键移动一格,当它与盒子精灵碰撞时,我需要在盒子的所有 4 个侧面的推动方向上“推动”精灵一格。
The game mechanics are similar to the game "baba is you" if that helps.如果有帮助的话,游戏机制类似于“baba is you”游戏。
So i have tried to just use rigid body and box colliders for the box and player, and adjusted the mass and drag of the box, but it just pushes the box a bit and not a defined distance.所以我尝试只为盒子和玩家使用刚体和盒子碰撞器,并调整盒子的质量和阻力,但它只是稍微推动盒子而不是定义的距离。 However i just want to detect the player from a side of the box and then push it by one unit/tile each time as the player moves one tile.
但是我只想从盒子的一侧检测玩家,然后每次玩家移动一个瓷砖时将其推一个单位/瓷砖。
I'm thinking that i need to attach a script to the box object to check if the player has hit the box and then just move the box in x or y direction?我在想我需要在盒子 object 上附加一个脚本,以检查玩家是否击中了盒子,然后将盒子沿 x 或 y 方向移动?
You are looking for linear interpolation (LERP for short).您正在寻找线性插值(简称 LERP)。 You can lerp an object using the rigidbody or transform.
您可以使用刚体或变换来处理 object。 Lerping allows you to interpolate the position over time.
Lerping 允许您随时间插值 position。 Here's an example:
这是一个例子:
transform.position = Vector3.Lerp(start, end, t);
In the above code, when t = 0, the position will be at the start.在上面的代码中,当 t = 0 时,position 将在开头。 When t=0.5, it will be exactly halfway between the start and end.
当 t=0.5 时,正好在起点和终点的中间。 When t = 1, it will be at the end.
当 t = 1 时,它将在末尾。 If you use this functionality with Time.deltaTime you can smoothly move your object between 2 points over time, making sure your start and end destinations are pinpoint specific.
如果您将此功能与 Time.deltaTime 一起使用,您可以随着时间的推移在 2 个点之间平滑移动 object,确保您的起点和终点是特定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.