改变射弹角度
Change projectile angle
我正在努力提高游戏的准确性。目前我的播放器将始终直接向前射击(指向鼠标光标)。我想将发射角度偏移 x 度。
我的触发脚本目前是这样的:
nextFire = Time.time + bulletConfig.TimeBetweenShots;
var offset = new Vector3(0, 0, 0);
var grid = GameObject.FindObjectOfType<Grid>();
var proj = Instantiate(projectile, transform.position, Quaternion.identity, grid.transform);
proj.transform.position = transform.position + offset;
proj.transform.rotation = transform.rotation;
print(proj.transform.rotation);
var controller = proj.GetComponent<BulletController>();
if (controller != null)
{
controller.Fire(bulletConfig);
}
Destroy(proj, bulletConfig.DestroyTime);
我的问题的核心是我不知道如何在没有一些复杂的三角函数的情况下将度数添加到 vector3。
有什么想法吗?
三角学并不十分复杂,尤其是当您拥有可以为您进行计算的转换对象时。 "Adding degrees" 相当于使用 Rotate 函数旋转弹丸的变换。
Float degrees = 5;
Quaternion q = Quaternion.AngleAxis(Vector3.right, degrees);
proj.transform.rotation = q * proj.transform.rotation;
// Alternatively, if you have a vector vecToRotate:
vecToRotate = q * vecToRotate;
这会将它向上移动 5 度。使用 -5 向下。其他方向请使用 Vector3.right
以外的内容。
如评论中所述:
Transform.rotate
state: "To rotate an object, use Transform.Rotate
的文档。"
修改您的示例,如下所示:
// -- snipped for brevity
var proj = Instantiate(projectile, transform.position, Quaternion.identity, grid.transform);
proj.transform.position = transform.position + offset;
proj.transform.rotation = transform.rotation;
// Using the second overload of Transform.Rotate
float exampleOffsetAngle = 1.0f;
proj.transform.Rotate(exampleOffsetAngle, 0.0f, 0.0f);
print(proj.transform.rotation);
// -- snipped for brevity
其他重载的更多示例和用法请参考官方文档:https://docs.unity3d.com/ScriptReference/Transform.Rotate.html
我正在努力提高游戏的准确性。目前我的播放器将始终直接向前射击(指向鼠标光标)。我想将发射角度偏移 x 度。
我的触发脚本目前是这样的:
nextFire = Time.time + bulletConfig.TimeBetweenShots;
var offset = new Vector3(0, 0, 0);
var grid = GameObject.FindObjectOfType<Grid>();
var proj = Instantiate(projectile, transform.position, Quaternion.identity, grid.transform);
proj.transform.position = transform.position + offset;
proj.transform.rotation = transform.rotation;
print(proj.transform.rotation);
var controller = proj.GetComponent<BulletController>();
if (controller != null)
{
controller.Fire(bulletConfig);
}
Destroy(proj, bulletConfig.DestroyTime);
我的问题的核心是我不知道如何在没有一些复杂的三角函数的情况下将度数添加到 vector3。
有什么想法吗?
三角学并不十分复杂,尤其是当您拥有可以为您进行计算的转换对象时。 "Adding degrees" 相当于使用 Rotate 函数旋转弹丸的变换。
Float degrees = 5;
Quaternion q = Quaternion.AngleAxis(Vector3.right, degrees);
proj.transform.rotation = q * proj.transform.rotation;
// Alternatively, if you have a vector vecToRotate:
vecToRotate = q * vecToRotate;
这会将它向上移动 5 度。使用 -5 向下。其他方向请使用 Vector3.right
以外的内容。
如评论中所述:
Transform.rotate
state: "To rotate an object, use Transform.Rotate
的文档。"
修改您的示例,如下所示:
// -- snipped for brevity
var proj = Instantiate(projectile, transform.position, Quaternion.identity, grid.transform);
proj.transform.position = transform.position + offset;
proj.transform.rotation = transform.rotation;
// Using the second overload of Transform.Rotate
float exampleOffsetAngle = 1.0f;
proj.transform.Rotate(exampleOffsetAngle, 0.0f, 0.0f);
print(proj.transform.rotation);
// -- snipped for brevity
其他重载的更多示例和用法请参考官方文档:https://docs.unity3d.com/ScriptReference/Transform.Rotate.html