改变弹丸角度

问题描述 投票:0回答:3

我正在尝试为游戏增加准确性。目前我的播放器将始终直接向前发射(指向鼠标光标)。我想将这个射击角度偏移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添加度数。

有任何想法吗?

c# unity3d angle
3个回答
1
投票

正如评论中所述:

Transform.rotate状态的文档:“要旋转对象,请使用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


0
投票
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之外的其他东西用于其他方向。


-1
投票

三角函数并不是很复杂,特别是当你有一个可以为你做计算的变换对象时。 “添加度”相当于使用Rotate函数旋转射弹的变换。

© www.soinside.com 2019 - 2024. All rights reserved.