Question by
fahadbilal · Jun 25, 2020 at 04:14 PM ·
projectiles
how bullet pointing towards the target unity
public transform[] targets;
indnum = Random.Range(0, targets.Length);
bullet = Instantiate(bulletpref, firepoint.position, firepoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.velocity = (targets[indnum].position - firepoint.position).normalized * bulletforce;
the code i use to fire its works perfect but the bullet is not pointing towards the target rather it go like parallel to it
Comment
Answer by Hellothere_1 · Jun 26, 2020 at 09:17 AM
You need to set Transform.Rotation of the bullet so it looks towards the target. The easiest way to do that is during initiation:
Vector3 dir = (targets[indnum].position - firepoint.position).normalized;
bullet = Instantiate(bulletpref, firepoint.position, Quaternion.LookRotation(dir));
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.velocity = dir * bulletforce;
Your answer
Follow this Question
Related Questions
How to only make one explosion spawn on impact. 1 Answer
Why are my instantiated projectiles not firing? 0 Answers
How to make my snowball model to actually shoot like a projectile? 0 Answers
Projectile disappears instantly after appearing. 1 Answer
2D Weapon Rotation Behaviour Changes on Flipping Sprite? 1 Answer