Getting 2D Turrets to rotate toward bullet direction.
I'm trying to get my turret to rotate toward the bullet direction. I can't seem to figure it out. My code is below for the bullet shooting out of the turret are below. The left and right floats are public variables I enter in for individual turrets. I have them shooting randomly within the confines of left and right. Can anyone point me in the right direction? Thank you!
public class TurretScript : MonoBehaviour {
public float xRange;
public float yRange;
public GameObject bullet;
public float bulletSpeed;
public float startTime;
public float bulletVelocity;
// Use this for initialization
void Start () {
InvokeRepeating ("FireBullet", startTime, bulletSpeed);
}
// Update is called once per frame
void Update () {
}
public void FireBullet()
{
float random = Random.Range(xRange, yRange);
GameObject clone;
clone = (Instantiate(bullet, transform.position, transform.rotation)) as GameObject;
clone.GetComponent<Rigidbody2D>().AddForce(new Vector2(random, bulletVelocity));
}
}
capture.png
(9.7 kB)
Comment
Answer by hauk70 · Nov 13, 2015 at 06:16 PM
If i right translate u try rotate turrets to bullet direction use LookAt(); If u try create dispersion in shooting find turrets direction, calculate min,max direction(use Quaternion) and find random on interval