简体   繁体   English

你如何将一个游戏 object 与另一个游戏 object 推送一个设定的距离,例如 1 个瓷砖

[英]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.

相关问题 我如何使游戏对象跟随另一个游戏对象但不移动到目标对象中? - How do i get the game object to follow another game object but not move into the targeted object? 如何设置物体在游戏物体方向上的速度? - How do I set the velocity of an object in the direction of the game object? 你如何获得玩家游戏 object 旋转与球体游戏 object - How do you get the player game object from rotating with the sphere game object Unity-如何将动画从一个游戏对象应用于另一个装备的游戏对象? - Unity - How do I apply an animation from one game object to another rigged game object? C#如何将模糊输入的对象(例如“ var”)作为特定类型的对象(例如“ Form”)访问 - C# How to access an ambiguously typed object (e.g. 'var') as a specifically typed object (e.g. 'Form') 如何让一个游戏object画线到另一个类似的游戏对象? - How to make a game object draw lines to another similar game objects? 纸牌游戏如何统一设置价值和适合纸牌游戏对象? - Card Game In unity how to set value and suit on card game object? 如何获取列表中每个游戏对象的距离,碰撞和方向? - How to get the Distance, Collision and Direction for each game object in my list? 如何使游戏对象始终以恒定的距离向前跳跃? - How to make a game object consistently jump forward with a constant distance? Unity中游戏对象与地面之间的距离 - Distance between game object and ground in Unity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM