使用物理学扔物体?(统一) [英] Throw an object using physics? (Unity)

查看:69
本文介绍了使用物理学扔物体?(统一)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已经工作了 4 个多小时,我正在尝试制作一个玩家可以扔的物体.我想扔它,(不要扔得太远,比如 0.5 米,)但我似乎无法得到它.我尝试通过刚体添加力,制作动画,并仅更改其变换.如果有人必须做类似的事情,有人可以帮助我吗?如果是,你是怎么做的?

So, I have been working for 4+ hours now, and I am trying to make an object that the player can throw. I was thinking of like tossing it, (not throwing it far, like .5 meters,) and I can’t seem to get it. I have tried adding force through rigidbody, making an animation, and just changing its transform. Can anyone help me if has anyone had to do something similar, if so how did you do it?

我没有任何代码示例,因为没有任何效果.(请不要只说刚体,你能举个例子吗.谢谢.)

I don’t have any code examples, because nothing worked. (Please don’t just say Rigidbody, can you give examples. Thanks.)

推荐答案

由于声誉原因,我仍然无法添加评论,因此这里是设置基于鼠标输入抛出的刚体的通用方法.

I am still unable to add comments due to reputation, so here is a generic approach to setting up a rigidbody to be thrown based on mouse input.

 Camera cam;
 Rigidbody r;
 Vector3 direction = cam.ScreenPointToRay(Input.mousePosition);
 r.AddForce(direction * 100);

代码使用场景的相机,要抛出的物体的刚体,然后将鼠标输入的屏幕空间转换为光线投射以获得抛出物体的方向,然后在那个方向的那个物体.

The code is using the camera of your scene, the rigidbody of the object you want to throw, it then converts the screen space of the mouse input to a raycast to get a direction to throw the object, then adds a force on that object in that direction.

如果您的代码看起来像这样,则很有可能您正在其他地方更改对象的速度或变换.它也可能与另一个物体发生碰撞并被停止.

If you code looks something like this, there is a good chance you are changing the velocity or transform of the object elsewhere. It could also be colliding with another object and be getting stopped.

这篇关于使用物理学扔物体?(统一)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆