简体   繁体   English

如何拉回以拍摄 2D 精灵? (C# + 统一)

[英]How Can I Pull Back To Shoot A 2D Sprite? (C# + Unity)

I'm having a bit of trouble working out how to get a simple pull back to shoot script on my 2D circle sprite, I know the idea behind it, but I can't work out the code exactly.我在弄清楚如何在我的 2D 圆形精灵上获得一个简单的回拉来拍摄脚本时遇到了一些麻烦,我知道它背后的想法,但我无法准确地计算出代码。

I've got this half working using the angry birds clone script inside of Unity's 2D pack asset, but that relies on another object and linked with a spring joint, it works in a way, but if I try to shoot it slowly and on specific angles, it will shoot backwards instead of the way it was intended to go.我在 Unity 的 2D 包资产中使用愤怒的小鸟克隆脚本完成了这一半工作,但这依赖于另一个对象并与弹簧关节链接,它在某种程度上起作用,但是如果我尝试在特定的情况下缓慢地拍摄它角度,它将向后射击,而不是按原定的方向射击。

Also I need the script to be just on the ball itself so that when the ball stops moving, it can be shot again from that location.此外,我需要将脚本放在球本身上,以便当球停止移动时,可以从该位置再次射门。

Here's what I'm thinking is the solution but can't work out the exact code for:这是我正在考虑的解决方案,但无法计算出以下确切代码:

Mathf.Atan2(ballStartY - ballCurrentY, ballStartX - ballCurrentX). Mathf.Atan2(ballStartY - ballCurrentY, ballStartX - ballCurrentX)。 Then maybe flip it by adding 180 somehow?然后也许可以通过以某种方式添加 180 来翻转它?

Then use the angle to break it down into x and y components (xSpeed = cos(angle) * force) and (ySpeed = sin(angle) * force) perhaps?然后使用角度将其分解为 x 和 y 分量(xSpeed = cos(angle) * force)和(ySpeed = sin(angle) * force)?

I also need to use the mouse position on click like I've got happening to move the ball and make it kinematic while it's being clicked on and also lock it so you can't pull it back further than a specified float amount.我还需要在点击时使用鼠标位置,就像我碰巧移动球并使其在点击时运动并锁定它,这样您就无法将其拉回超过指定的浮动量。

I really appreciate any help you can give!我真的很感激你能提供的任何帮助! Thanks everyone!谢谢大家!

If I understand correctly you have Three problems making the object move, letting the object be re-thrown when it is stopped,and how to pull the object to launch it.如果我理解正确,您会遇到三个问题:使物体移动,让物体在停止时重新投掷,以及如何拉动物体以发射它。

So for the first one if you rotate the ball so that the x axis of the local cords is in the direction you want to shoot then you can use this code to make the ball fly forward at some magnitude indicated by the vector3.因此,对于第一个,如果您旋转球以使局部绳索的 x 轴在您想要射击的方向上,那么您可以使用此代码使球以向量 3 指示的某个幅度向前飞行。 The force mode is impulse because you want instantaneous force.力模式是脉冲,因为您需要瞬时力。

gameObject.GetComponent<Rigidbody>().AddRelativeForce(9,0,0, ForceMode.Impulse);

The rotation should be simple with the look at function.使用查看功能,旋转应该很简单。 The documentation of such is here you then rotate that 180 with the quaternion function.此类文档在 这里,然后您可以使用四元数函数旋转 180 度。 you use that with the mouse position to get something like this:你用它和鼠标位置来得到这样的东西:

rotation = Quaternion.Euler(180, 0, 0) * Input.mousePosition;
transform.LookAt(Rotation);

Then just use an if for finding out if you are clicking to activate it.然后只需使用 if 来确定您是否单击以激活它。 If you want to show it being pulled back use Vector3.Distance with an if statement like:如果您想显示它被拉回,请使用 Vector3.Distance 和 if 语句,例如:

if(Vector3.Distance(thisGameobeject, someParentObjectThatWontMoveWhenPulledBack)<50)
{pull further back}

As for checking if your are stopped you could use an if statement with Rigidbody.velocity and that should just about cover everything.至于检查您是否停止,您可以使用带有 Rigidbody.velocity 的 if 语句,这应该涵盖所有内容。 I hope this at least helps a little bit.我希望这至少会有所帮助。

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

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