简体   繁体   English

unity用鼠标拖放游戏对象(例如:sprite)

[英]Unity drag and throw a gameobject (ex: sprite) with mouse

I want to throw a gameObject (like a sprite) with my mouse. 我想用鼠标扔一个gameObject(像一个精灵)。 I would like to handle to power if the mouvement is fast. 如果移动速度很快,我想控制电源。

I've try to develop it myself but I haven't found yet. 我已经尝试自己开发它,但尚未找到。

In my mind I think I need to do this : 我认为我需要这样做:

  • on mouse down : get the position 在鼠标按下时:获取位置
  • 0.5secs after of on mouse up : get the new position and calculate the distance between the 2 points. 鼠标悬停0.5秒后:获得新位置并计算两点之间的距离。 Conclude a direction and a force. 得出方向和作用力。

What do you think about this ? 你怎么看待这件事 ?

I created something fairly similar to this. 我创建了与此类似的东西。 You should do OnMouseDown() then Raycast on the position of the mouse click. 您应该先执行OnMouseDown()然后在鼠标单击的位置进行Raycast Then your Raycast should grab the collider of the GameObject that was hit. 然后,您的Raycast应该抓住被击中的GameObject的对撞机。 Then you send a message, SendMessage , to that collider telling it what you want to do. 然后,您向该对撞机发送一条消息SendMessage ,告诉它您想做什么。

All the links I have given you and some trial an error will help you construct what you need, that is how I created my level editor which is in essence the same thing. 我提供给您的所有链接和一些试验错误会帮助您构建所需的内容,这就是我创建关卡编辑器的方式,本质上是一样的。 Note that all links have code samples that teach you all you need to do. 请注意,所有链接的代码示例都可以教您所有需要做的事情。 Just use your imagination and you can get it done! 只需发挥您的想象力,您就可以完成它!

Just start coding. 刚开始编码。 If you get stuck then ask a more specific question and someone will help you. 如果您遇到困难,请提出一个更具体的问题,然后有人会帮助您。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM