- Home /
Problem is not reproducible or outdated
Rotation and Velocity of bullets.
Hey you all!
So, I'm working in 2D. I've got a bullet firing from a turret that can rotate. The bullet is supposed to determine its movement vector based on where the mouse was when it was fired -- but I'm having trouble getting the bullet to both rotate in the direction it's going and go in that direction at the same time. Basically, I can have one or the other, but not both -- it's because if I orientate the bullet to its movement direction at all, it changes the angle somehow. So, if I comment out the line that assigns the angle, the bullet moves correctly. But when it's in, the bullets rotate correctly. But they always end up going "down" on the y axis. Here's the function -- what am I doing wrong, do you think?
public void Shoot(float accuracyVariance, ShotType type) {
if(_currentChargeObject)
Destroy(_currentChargeObject);
//determine prefab.
GameObject shotPrefab = _bulletShotPrefab;
if(type == ShotType.Exploding)
shotPrefab = _missilePrefab;
//get correct pos.
Vector3 pos = _bulletStartPoint.transform.position;
pos.z = _markerCollisionZ.transform.position.z;
//instantiate.
GameObject shot = (GameObject) Instantiate(shotPrefab, pos, Quaternion.identity);
//add velocity.
Vector3 firingVector = _normalisedMovementVector;
if(accuracyVariance > 0)
firingVector = GetFiringVectorWithInaccuracy(accuracyVariance, _normalisedMovementVector);
shot.GetComponent<BulletMovementBehavior>().UpdateVelocity(firingVector * _shotSpeed);
//get correct rot.
Vector3 rot = _bulletStartPoint.transform.eulerAngles;
rot.z = VectorToATan(_bulletStartPoint.transform.position, _reticle.transform.position);
shot.transform.eulerAngles = rot;
//print(firingVector);
//bullet behavior.
if(type == ShotType.Exploding)
shot.GetComponent<MissileBehavior>()._explosionPoint = _reticle.transform.position;
GenerateShell();
}
public float VectorToATan(Vector3 currPos, Vector3 destPos) {
float opp = currPos.x - destPos.x;
float adj = currPos.y - destPos.y;
return Mathf.Rad2Deg * Mathf.Atan2(opp, adj);
}
Answer by Catlard · Jun 11, 2013 at 07:38 AM
Figured it out. The bullet was moving by using transform.Translate() every frame. Instead, i'm just adding the vector to the position -- this works nicely. transform.Translate() by default seems to move in the local space.