Unity平台臂旋转与武器偏移

时间:2016-03-17 21:51:31

标签: unity3d rotation trigonometry

你好我在计算我的角色手臂的旋转时遇到了麻烦,因为枪支从肩部枢轴偏移,以便将枪口对准鼠标。但是当它旋转手臂时,它还必须更新新的武器偏移量,如果鼠标位置+武器偏移量接近枪口,则枪支旋转会抖动。

public void rotateArm(){
    Vector3 centerPosition = armPivotPos.position;
    Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition)+updatedWeaponOffset();
    mousePosition.z = armPivotPos.position.z;
    Vector3 dir = centerPosition - mousePosition;
    float angle = Mathf.Atan2(dir.y, dir.x) * -Mathf.Rad2Deg;
    Quaternion temp = Quaternion.AngleAxis(angle, -Vector3.forward);
    armPivotPos.eulerAngles = new Vector3(temp.eulerAngles.x, temp.eulerAngles.y, temp.eulerAngles.z);  
}
public vector3 updatedWeaponOffset(){
    Vector3 weaponOffset = armPivotPos.position - muzzlePos.position;   
    return weaponOffset;
}

enter image description here

0 个答案:

没有答案