在Unity中以角度向上方添加力的方法是什么?

huangapple go评论60阅读模式
英文:

How to add force in up direction at an angle in Unity

问题

使用以下代码在对象的上方以略微倾斜的角度(约30度)添加力:

float angleInDegrees = 30f;
Vector2 forceDirection = Quaternion.Euler(0, 0, angleInDegrees) * ship.transform.up;
ammo.GetComponent<Rigidbody2D>().AddForce(forceDirection * magnitude);

请注意,这将在给定角度上添加力,使弹药朝着船只的方向以30度倾斜的角度移动。

英文:

I am adding force in up direction of an object using
ammo.GetComponent&lt;Rigidbody2D&gt;().AddForce(ship.transform.up * magnitude);
This way the ammo will always follow the direction of ship. How can i add similar force in up direction but with a slight angle, around 30 degrees?

答案1

得分: 1

你可以简单地使用船舶的旋转,并将所需旋转添加到其中,如:

ammo.GetComponent&lt;Rigidbody2D&gt;().AddForce(ship.transform.rotation * Quaternion.Euler (-30, 0, 0) * Vector3.up * magnitude);

它将采用 up 方向,并首先根据船舶的方向进行旋转,然后在本地x轴上再添加-30度。

英文:

You can simply use the ships rotation and add your desired rotation to it like e.g.

ammo.GetComponent&lt;Rigidbody2D&gt;().AddForce(ship.transform.rotation * Quaternion.Euler (-30, 0, 0) * Vector3.up * magnitude);

which takes the up direction and first rotates it according to the ships orientation and then from there adds another -30° on the local x-axis

huangapple
  • 本文由 发表于 2023年5月17日 19:34:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271649.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定