让物品在 2D、自上而下的世界中弹跳
Get an item to bounce in a 2D, top-down, world
在我的 2D 世界中,自上而下(有角度)。当我砍树、挖掘节点、杀死敌人等时,世界中会生成物品;原木、矿石、武器等
几天来我一直在尝试让它们在生成(出现在世界中)时表现得像这张图片:
即采取随机方向,"bouncing",同时减速并最终停止。
我在我的 RigidBody2D
上使用 Impulse
和 gravity
对 AddForce
进行了很多实验,然后在脚本应该停止时将其设置为 0。但是我无法得到我想要的效果。
假设我有一个名为 SpawnItem()
的方法,该方法在 Start()
上调用,将此移动添加到我的项目中,可能仅持续 0.5-1 秒。我怎么能做到这一点?如果不可能在所有(随机)方向上获得它,那么向左或向右就可以了。
关于这个主题,我能找到的所有内容都涉及横向卷轴,您可以在其中轻松地将其从对撞机上弹开。但是在自上而下你不能那样做。
编辑:这是一个视频,展示了我想要的(几乎)确切的行为:https://www.youtube.com/watch?v=LxvYHsEJi9U
一个好方法可能是为你想要生成的游戏对象创建一个动画,它只是它的精灵上下移动,当你生成你的游戏对象时,只需在播放动画时将它推向你想要的方向.
这里有一篇关于获得很酷的自上而下弹跳效果的好文章:
不知道隔了很久你会不会看到我的评论
我找到了一些代码并改进了代码所以这是我的完整脚本:
trnsObject = trnsBody,你把什么拖到trnsObject,把同一个对象拖到trnsBody
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class BounceDue : MonoBehaviour
{
public UnityEvent onGroundHitEvent;
public Transform trnsObject;
public Transform trnsBody;
public float gravity = -10;
public Vector2 groundVelocity;
public float verticalVelocity;
private float lastVerticalVelocity;
public bool isGrounded;
private float randomYDrop;
float firstYPos;
private void Awake()
{
}
void Start()
{
randomYDrop = Random.Range(-1f, 1f);
firstYPos = transform.position.y;
Set(Vector3.right * Random.Range(-1, 2) * Random.Range(1f, 3f), Random.Range(2f, 5f));
}
void Update()
{
UPosition();
CheckGroundHit();
}
public void Set(Vector2 groundVelocity, float verticalVelocity)
{
isGrounded = false;
this.groundVelocity = groundVelocity;
this.verticalVelocity = verticalVelocity;
lastVerticalVelocity = verticalVelocity;
}
public void UPosition()
{
if (!isGrounded)
{
verticalVelocity += gravity * Time.deltaTime;
trnsBody.position += new Vector3(0, verticalVelocity, 0) * Time.deltaTime;
}
trnsObject.position += (Vector3)groundVelocity * Time.deltaTime;
}
void CheckGroundHit()
{
if (trnsBody.position.y < firstYPos- randomYDrop && !isGrounded)
{
trnsBody.position = new Vector2(trnsObject.position.x, firstYPos - randomYDrop);
isGrounded = true;
GroundHit();
}
}
void GroundHit()
{
onGroundHitEvent.Invoke();
}
public void Bounce(float division)
{
Set(groundVelocity, lastVerticalVelocity / division);
}
public void SlowDownVelocity(float division)
{
groundVelocity = groundVelocity / division;
}
}
And look at the script settings for onGroundEvent
在我的 2D 世界中,自上而下(有角度)。当我砍树、挖掘节点、杀死敌人等时,世界中会生成物品;原木、矿石、武器等
几天来我一直在尝试让它们在生成(出现在世界中)时表现得像这张图片:
即采取随机方向,"bouncing",同时减速并最终停止。
我在我的 RigidBody2D
上使用 Impulse
和 gravity
对 AddForce
进行了很多实验,然后在脚本应该停止时将其设置为 0。但是我无法得到我想要的效果。
假设我有一个名为 SpawnItem()
的方法,该方法在 Start()
上调用,将此移动添加到我的项目中,可能仅持续 0.5-1 秒。我怎么能做到这一点?如果不可能在所有(随机)方向上获得它,那么向左或向右就可以了。
关于这个主题,我能找到的所有内容都涉及横向卷轴,您可以在其中轻松地将其从对撞机上弹开。但是在自上而下你不能那样做。
编辑:这是一个视频,展示了我想要的(几乎)确切的行为:https://www.youtube.com/watch?v=LxvYHsEJi9U
一个好方法可能是为你想要生成的游戏对象创建一个动画,它只是它的精灵上下移动,当你生成你的游戏对象时,只需在播放动画时将它推向你想要的方向.
这里有一篇关于获得很酷的自上而下弹跳效果的好文章:
不知道隔了很久你会不会看到我的评论
我找到了一些代码并改进了代码所以这是我的完整脚本:
trnsObject = trnsBody,你把什么拖到trnsObject,把同一个对象拖到trnsBody
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class BounceDue : MonoBehaviour
{
public UnityEvent onGroundHitEvent;
public Transform trnsObject;
public Transform trnsBody;
public float gravity = -10;
public Vector2 groundVelocity;
public float verticalVelocity;
private float lastVerticalVelocity;
public bool isGrounded;
private float randomYDrop;
float firstYPos;
private void Awake()
{
}
void Start()
{
randomYDrop = Random.Range(-1f, 1f);
firstYPos = transform.position.y;
Set(Vector3.right * Random.Range(-1, 2) * Random.Range(1f, 3f), Random.Range(2f, 5f));
}
void Update()
{
UPosition();
CheckGroundHit();
}
public void Set(Vector2 groundVelocity, float verticalVelocity)
{
isGrounded = false;
this.groundVelocity = groundVelocity;
this.verticalVelocity = verticalVelocity;
lastVerticalVelocity = verticalVelocity;
}
public void UPosition()
{
if (!isGrounded)
{
verticalVelocity += gravity * Time.deltaTime;
trnsBody.position += new Vector3(0, verticalVelocity, 0) * Time.deltaTime;
}
trnsObject.position += (Vector3)groundVelocity * Time.deltaTime;
}
void CheckGroundHit()
{
if (trnsBody.position.y < firstYPos- randomYDrop && !isGrounded)
{
trnsBody.position = new Vector2(trnsObject.position.x, firstYPos - randomYDrop);
isGrounded = true;
GroundHit();
}
}
void GroundHit()
{
onGroundHitEvent.Invoke();
}
public void Bounce(float division)
{
Set(groundVelocity, lastVerticalVelocity / division);
}
public void SlowDownVelocity(float division)
{
groundVelocity = groundVelocity / division;
}
}
And look at the script settings for onGroundEvent