简体   繁体   English

基于方向的刚体2d等速

[英]rigidbody2d constant velocity based on direction

I currently have this script (don't read too much into the code, not everything is pasted here, only what is needed to understand the question): 我目前有这个脚本(不要在代码中读太多,不是所有内容都粘贴在这里,只有理解问题所需的内容):

Touch[] touches = Input.touches;
Vector2 dir = (Camera.main.ScreenPointToRay (touches [i].position).GetPoint (0) - obj.getGameObject ().transform.position).normalized * speed;
obj.getGameObject ().GetComponent<Rigidbody2D> ().velocity = dir;

The problem is that the speed of the rigidbody is different based on distance from the object to the touched point, so the speed will be 2x or more if you touch a point distant from the gameobject, compared to a point close to the gameobject. 问题在于,刚体的速度基于从对象到触摸点的距离而不同,因此,如果您触摸到远离游戏对象的点,则该速度将是接近游戏对象的点的2倍或更多。

How would I go about fixing this, so I can achieve a constant velocity which isn't based on distance from the gameobject? 我将如何解决这个问题,以便获得不依赖于与游戏物体距离的恒定速度?

The core of the problem lies within this line: 问题的核心在于以下方面:

Vector2 dir = (Camera.main.ScreenPointToRay (touches [i].position).GetPoint (0) - obj.getGameObject ().transform.position).normalized * speed;

The direction vector is denormalized due to a cast 方向向量由于强制转换而归一化

When you calculate this difference between the touch position and the object position with Camera.main.ScreenPointToRay (touches [i].position).GetPoint (0) - obj.getGameObject ().transform.position , you have to remember that this result may not be 2D - it is a Vector3 . 当您使用Camera.main.ScreenPointToRay (touches [i].position).GetPoint (0) - obj.getGameObject ().transform.position计算触摸位置和对象位置之间的这种差异时,您必须记住该结果可能不是2D,而是Vector3 This means that it may have a non-zero z-value, if the result of GetPoint (0) and transform.position are not at the same position on the z-axis. 这意味着,如果GetPoint (0)transform.position结果不在z轴上的同一位置,则z值可能不为零。

When you normalize the Vector3 , its length becomes 1, but only if its z-value is taken into account. 规范化Vector3 ,其长度变为1,但Vector3是要考虑其z值。 When you store this value in Vector2 dir , that Vector3 is cast to a Vector2 and its z-value is discarded. 当您将此值存储在Vector2 dir ,该Vector3Vector2转换为Vector2并且其z值将被丢弃。 This means that the length of the previously-normalized vector is no longer guaranteed to be 1; 这意味着先前归一化向量的长度不再保证为1; it can vary anywhere in the range of [0,1]. 它可以在[0,1]范围内的任何地方变化。

The cast vector's length depends on the relative size of the z-value 转换向量的长度取决于z值的相对大小

If you multiply this new Vector2 with speed and use it to set the velocity of a rigidbody, the magnitude of the rigidbody's velocity will be between 0 and speed , rather than always speed . 如果将此新Vector2乘以speed然后用它来设置刚体的速度,则刚体速度的大小将介于0和speed之间,而不是始终为speed The reason why the object goes faster if your touch is farther, and slower if it is closer, is because the z-value (which will be constant) will become less relative to your x- and y-values the farther away your touch is. 如果您的触摸距离越远,对象移动得越快,而如果您的触摸距离越近,对象移动得越慢的原因是,z值(将保持不变)相对于您的触摸距离越远的x和y值会变得越小。 Here's an illustration to show how this will affect the cast Vector2 : 这是说明这将如何影响Vector2

在此处输入图片说明

Note how casting the normalized Vector3 will result in a longer Vector2 if the z-value is smaller relative to the other values. 请注意,如果z值相对于其他值较小,则强制转换归一化Vector3会导致更长的Vector2 (The diagram assume x = 0, to make it easier to read.) (该图假定x = 0,以便于阅读。)

The solution 解决方案

Fixing this problem isn't too difficult - you just need to normalize the vector once the z-value has become 0, whether by cast or by explicit assignment. 解决这个问题并不是太困难-只要z值变为0,无论是通过强制转换还是通过显式赋值,您都只需对向量进行归一化。 Then your direction is guaranteed to be a length of 1, and your speed will be constant. 然后,您的方向将保证为1的长度,并且速度将保持恒定。 Here's an example: 这是一个例子:

Touch[] touches = Input.touches;
Vector3 dirRaw = Camera.main.ScreenPointToRay (touches [i].position).GetPoint (0) - obj.getGameObject ().transform.position;
dirRaw.z = 0;
Vector2 dir = dirRaw.normalized * speed;
obj.getGameObject ().GetComponent<Rigidbody2D> ().velocity = dir;

Hope this helps! 希望这可以帮助! Let me know if you have any questions. 如果您有任何疑问,请告诉我。

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

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