I have a sprite that I want to use for a bullet in 3d space and I want it to constantly face the players transform (sort of like the monsters in doom always look at you).
I’ve tried attaching my sprite to a plane, but I can’t seem to figure out how to move it in world space.
I don’t want to have to use InverseTransformDirection for each frame, since I’m expecting to have a lot of bullets, so I would like to have the movement vector planned out during the start function.
Is there some other, better way to use an image as a bullet ? If not, how would I make my plane always rotate towards the player, while still moving the same direction in world space?
1 Answer
1
So a plane draws it’s visible face upwards - to billboard you need to point that upwards face in the opposite direction to Camera.main.transform.forward.
Your bullet script will need a direction in it as you can’t use it’s rotation because that’s dependent on the viewing angle.
Quaternion rotateUpToForward = Quaternion.FromToRotation(Vector3.up, Vector3.forward);
public Vector3 bulletDirection;
public float speed;
static Transform mainCameraTransform;
void Start()
{
mainCameraTransform = Camera.main.transform;
}
void Update()
{
transform.position += bulletDirection * speed;
transform.forward = rotateUpToForward * -mainCameraTransform.forward;
}
I wrote a tutorial on Quaternions over on Unity Gems covers a lot of this stuff
When you first the bullet you will probably set bulletDirection = the main camera's transform.forward
– whydoidoit