Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Enemy shooting at player

$
0
0
Hi guys! So, I'm working on a game level for college that's due this Friday, it's a runner-type level with a helicopter chasing and shooting the player, however I can't get the shooting script to work. I'll post my script here, mostly taken from this video: https://www.youtube.com/watch?v=fxjRtaV4N7g . If anyone can help me with this, please do so, this is **urgent** : public float playerRange; public GameObject player; public Transform launchPoint; public float bulletSpeed = 10; public Rigidbody bullet; void Start() { player = GameObject.FindGameObjectWithTag("Player"); } void Fire() { Rigidbody bulletClone = Instantiate(bullet, launchPoint.position, launchPoint.rotation); bulletClone.velocity = transform.forward * bulletSpeed; } void Update() { Debug.DrawLine(new Vector3(transform.position.x - playerRange, transform.position.y, transform.position.z), new Vector3(transform.position.x + playerRange, transform.position.y, transform.position.z)); Fire(); } Thanks, anyone who reads this! :D EDIT: Changed some code, here it is now still not working: void Start() { player = GameObject.FindGameObjectWithTag("Player"); } void Fire() { Instantiate(bullet, launchPoint.position, launchPoint.rotation); print("It should be firing at this point"); } // Update is called once per frame void Update() { Debug.DrawLine(new Vector3(transform.position.x - playerRange, transform.position.y, transform.position.z), new Vector3(transform.position.x + playerRange, transform.position.y, transform.position.z)); if (transform.localScale.x < 0 && player.transform.position.x > transform.position.x && player.transform.position.x < transform.position.x + playerRange) { Fire(); } } AND here's the bullet code I added, damage commented out for now: void Start () { } // Update is called once per frame void Update() { GetComponent().velocity = new Vector2(speed, GetComponent().velocity.y); } private void OnTriggerEnter2D(Collider2D other) { if(other.tag == "Player") { //PlayerCK.hurtPlayer(damage); } } (Global variables removed for formatting)

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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