要点一:射箭施加力的方式
arrow.GetComponent<Rigidbody>().AddForce (Quaternion.Euler (new Vector3(transform.rotation.eulerAngles.x,transform.rotation.eulerAngles.y,transform.rotation.eulerAngles.z))*new Vector3(25f*length,0,0), ForceMode.VelocityChange);
返回一个旋转角度,绕z轴旋转z度,绕x轴旋转x度,绕y轴旋转y度(像这样的顺序)
Quaternion.Euler(x,y,z) 返回一个绕x轴旋转x度再绕y轴旋转y度再绕z轴旋转z度的Quaternion, 因此Quaternion.Euler(0,90,0)返回一个绕y轴旋转90度的旋转操作.Quaternion作用于Vector3的右乘操作(*)返回一个将向量做旋转操作后的向量. 因此Quaternion.Euler(0,90,0)*Vector3(0.0,0.0,-10)表示将向量Vector3(0.0,0.0,-10)做绕y轴90度旋转后的结果.因该等于Vector3(-10,0,0).
ForceMode.VelocityChange
Unity中关于作用力方式ForceMode的功能注解
此种作用方式下将忽略刚体的实际质量,采用默认质量1.0,同时也忽略系统的实际帧频间隔,采用默认间隔1.0,即f•1.0=1.0•v
要点二:计算射箭方向
public void prepareArrow() {
// get the touch point on the screen
mouseRay1 = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(mouseRay1, out rayHit, 1000f) && arrowShot == false)
{
// determine the position on the screen
posX = this.rayHit.point.x;
posY = this.rayHit.point.y;
// set the bows angle to the arrow
Vector2 mousePos = new Vector2(transform.position.x-posX,transform.position.y-posY);
float angleZ = Mathf.Atan2(mousePos.y,mousePos.x)*Mathf.Rad2Deg;
transform.eulerAngles = new Vector3(0,0,angleZ);
// determine the arrow pullout
length = mousePos.magnitude / 3f;
length = Mathf.Clamp(length,0,1);
// set the bowstrings line renderer
stringPullout = new Vector3(-(0.44f+length), -0.06f, 2f);
// set the arrows position
Vector3 arrowPosition = arrow.transform.localPosition;
arrowPosition.x = (arrowStartX - length);
arrow.transform.localPosition = arrowPosition;
}
arrowPrepared = true;
}