英文:
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<Rigidbody2D>().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<Rigidbody2D>().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<Rigidbody2D>().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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论