我有一个LineRenderer由3个点组成的。我想从第二个点向第三个方向发射弹丸。我试图获得两个位置,在第二个实例化并朝第三个方向向前射击,但是我的射弹有点偏离了它们应该去的地方。有一种方法可以射击我以前使用的射弹,并且当我LineRenderer只有 2 分时有效。我从https://unity3d.com/learn/tutorials/topics/physics/brick-shooter得到了这个想法。public void Shoot(){ shotPosL = laserPointerL.GetComponent<Transform>(); //get the position of the beginning of the laser pointer Rigidbody shotL = Instantiate(projectile, shotPosL.position, shotPosL.rotation) as Rigidbody; //instantiate a new projectile shotL.AddForce(shotPosL.forward * shotForce); //throw the projectile in the direction of the laser pointer}但是由于我在我的 中添加了第三个点LineRenderer,射弹只是直接从屏幕中间向后移动。编辑:有人判断我的信息没有用或不清楚,对不起,很难解释。如果有帮助,有我当前的代码。public void Shoot(){ Quaternion noRotation = Quaternion.Euler(0, 0, 0); LineRenderer laserL = laserPointerL.GetComponent<LineRenderer>(); Vector3 kneeLeftPosition = laserL.GetPosition(1); Vector3 laserLeftTarget = laserL.GetPosition(2); Rigidbody shotL = Instantiate(projectile, kneeLeftPosition, noRotation) as Rigidbody; //instantiate a new projectile shotL.AddForce(laserLeftTarget * shotForce); //throw the projectile in the direction of the laser pointer}
2 回答
哔哔one
TA贡献1854条经验 获得超8个赞
假设你的点被定义为
Vector3 point1; Vector3 point2;
从 1 到 2 的力应该有一个方向 (point2-point1) - 你应该从终点减去起点以获得方向。采用
rb.AddForce.((point2-point1).normalized*shotForce);
- 2 回答
- 0 关注
- 165 浏览
添加回答
举报
0/150
提交
取消