- Home /
Question by
unity_lF98_nVJe9di1A · Apr 08, 2019 at 05:42 PM ·
rotationpositiontransform.position
Firing a shell from a tank which rotates
So I rotate a tank turret and want the shell to fire from an origin position that is just in front in the barrell. But because the barrell keeps rotating with the turret, I'm unable to obtain an origin position that is relative to the tank's rotation if that makes sense.
private void Fire ()
{
// Set the fired flag so only Fire is only called once.
m_Fired = true;
//set origin position of shell to be just infront of turret barrell
Vector3 shellOrigin = new Vector3(tankTurretTransform.position.x+1, tankTurretTransform.position.y, tankTurret.transform.position.z+1);
// m_FireTransform.position = new Vector3(tankTurretTransform.forward.x, tankTurretTransform.forward.y, tankTurretTransform.forward.z+0.3f);
// Create an instance of the shell and store a reference to it's rigidbody.
Rigidbody shellInstance =
Instantiate (m_Shell, shellOrigin, tankTurretTransform.rotation) as Rigidbody;
// Set the shell's velocity to the launch force in the fire position's forward direction.
shellInstance.velocity = m_CurrentLaunchForce * tankTurretTransform.forward;
// Change the clip to the firing clip and play it.
m_ShootingAudio.clip = m_FireClip;
m_ShootingAudio.Play ();
ThalmicMyo thalmicMyo = myo.GetComponent<ThalmicMyo>();
// Reset the launch force. This is a precaution in case of missing button events.
m_CurrentLaunchForce = m_MinLaunchForce;
}
With my code, the origin position of the shell does not make sense as I rotate the turret, and I end up blowing up my own tank.
tank-turret.png
(61.7 kB)
Comment
Best Answer
Answer by unity_lF98_nVJe9di1A · Apr 08, 2019 at 07:16 PM
Solved. There is a GameObject called FireTransform in the hierarchy that can be adjusted for this.
solution-to-question.png
(229.2 kB)
Answer by $$anonymous$$ · Apr 08, 2019 at 06:25 PM
Try transform.localPosition instead of transform.position.