首先,这里是问题的视频:
链接到视频,这里是包
,这里是层次结构的屏幕截图:
GUN 是带有脚本的父级(空游戏对象) 暂停被丢弃到towerRotateObj
并且 Gun 被丢弃在turretRotateObj
. Suspension 和 Gun 也是空的游戏对象。它们只是某种组对象,这里是代码:
public class WeaponMover : MonoBehaviour
{
public Transform target;
public GameObject turretRotateObj;
public GameObject towerRotateObj;
public float maxTowerRotationSpeed = 360.0f;
public float maxTurretRotationSpeed = 360.0f;
public float smoothFactorTower = 0.125f;
public float smoothFactorTurret = 0.125f;
public float maxTowerRotation = 130.0f;
public float maxTurretRotation = 50.0f;
private Vector3 m_newRotation;
private Vector3 m_angles;
private float m_minTowerAngle;
private float m_maxTowerAngle;
private float m_minTurretAngle;
private float m_maxTurretAngle;
private float m_velTower;
private float m_velTurret;
private bool m_isTransNecTower = false;
private bool m_isTransNecTurret = false;
// initialization
void Start()
{
m_newRotation = Vector3.zero;
m_angles = Vector3.zero;
m_maxTowerAngle = towerRotateObj.transform.eulerAngles.y + maxTowerRotation/2;
m_minTowerAngle = towerRotateObj.transform.eulerAngles.y - maxTowerRotation/2;
m_maxTurretAngle = turretRotateObj.transform.eulerAngles.z + maxTurretRotation/2;
m_minTurretAngle = turretRotateObj.transform.eulerAngles.z - maxTurretRotation/2;
// check if rotation happens between 0/360
// tower
if(m_minTowerAngle <= 0.0f)
m_minTowerAngle += 360.0f;
if(m_maxTowerAngle >= 360.0f)
m_maxTowerAngle -= 360.0f;
if(m_minTowerAngle > m_maxTowerAngle)
m_isTransNecTower = true;
// turret
if(m_minTurretAngle <= 0.0f)
m_minTurretAngle += 360.0f;
if(m_maxTurretAngle >= 360.0f)
m_maxTurretAngle -= 360.0f;
if(m_minTurretAngle > m_maxTurretAngle)
m_isTransNecTurret = true;
}
void Update()
{
m_newRotation = Quaternion.LookRotation(target.position - towerRotateObj.transform.position).eulerAngles;
m_angles = towerRotateObj.transform.rotation.eulerAngles;
towerRotateObj.transform.rotation = Quaternion.Euler(m_angles.x,
ClampAngle(Mathf.SmoothDampAngle(m_angles.y,
m_newRotation.y - 90.0f,
ref m_velTower,
smoothFactorTower,
maxTowerRotationSpeed), m_minTowerAngle, m_maxTowerAngle, m_isTransNecTower),
m_angles.z);
m_newRotation = Quaternion.LookRotation(target.position - turretRotateObj.transform.position).eulerAngles;
m_angles = turretRotateObj.transform.rotation.eulerAngles;
turretRotateObj.transform.rotation = Quaternion.Euler(m_angles.x,
m_angles.y,
ClampAngle(Mathf.SmoothDampAngle(m_angles.z,
-m_newRotation.x,
ref m_velTurret,
smoothFactorTurret,
maxTurretRotationSpeed), m_minTurretAngle, maxTurretRotation, m_isTransNecTurret));
}
private float ClampAngle(float angle, float min, float max, bool isTranslationNecessary)
{
if(!isTranslationNecessary)
{
if(angle < min )
return min;
if(angle > max)
return max;
}
else
{
if(angle > max && angle < min)
{
if(min - angle > angle - max)
return max;
else
return min;
}
}
return angle;
}
}
所以,它是一个类似的设置,就像坦克的塔和枪......我已经在这里发布了这个问题,但似乎很少有人看到这个帖子......任何建议都值得赞赏!谢谢。
更新:
m_newRotation = Quaternion.LookRotation(m_target.transform.position - towerRotateObj.transform.position).eulerAngles;
m_newRotation.y -= 90f;
m_angles = towerRotateObj.transform.rotation.eulerAngles;
towerRotateObj.transform.rotation = Quaternion.Euler(m_angles.x, m_newRotation.y, m_angles.z);
m_newRotation = Quaternion.LookRotation(m_target.transform.position - turretRotateObj.transform.position).eulerAngles;
m_angles = turretRotateObj.transform.rotation.eulerAngles;
turretRotateObj.transform.rotation = Quaternion.Euler(m_angles.x, m_angles.y, -m_newRotation.x);
问题保持不变:(