I have a sentry gun firing at the player’s current position. This works fine, but I’d like the projectile to be rotated towards the player too when it’s instantiated. A script attached to the turret instantiates the fireball:
Rigidbody2D fireProjectile = Instantiate (fireproj, new Vector3 (transform.position.x, transform.position.y, transform.position.z), transform.rotation) as Rigidbody2D;
How can I get transform.position to be the current position of the player?
Second, there appear to be no inherent problems with your lines unless the projectile is not oriented in the same way as your turret. For example, if the bullet and turret both naturally face upward, then there shouldn’t be any problem. If the turret faces right and the bullet faces left, then you’ll need to flip it around.
For Quaternions, that can be handled in as simple-looking of a presentation as:
transform.rotation * offsetRotation
As for the definition of “offsetRotation”, continuing on the basis of Quaternion values, you may consider tinkering with Quaternion.AngleAxis() for that.
On another note, you ask about the position of the player at the end. If your sentry gun is tracking the player’s position already, then you would already have that information on-hand. If not, then there arenumerousways of getting that information.
Direction = player.position - projectile.position. This will send your projectile straight to player. The just use the Unity Transform.lookat(player). Together your projectile will go the right way, and face the right direction.