Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

Getting 2D Turrets to rotate toward bullet direction.

$
0
0
![alt text][1] 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().AddForce(new Vector2(random, bulletVelocity)); } } [1]: /storage/temp/58089-capture.png

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>