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

Instantiate Bullet that points to the player

$
0
0
I'm new to unity and wrote this enemy AI script from a tutorial. Whenever I spawn in the bullet, it is always rotates towards (0, 0, 0) and the bullet ends up facing upwards. I would like to change it to always be rotated pointing towards the player when fired, can someone help? Here is my script, the instantiate bullet is near the bottom using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyAI : MonoBehaviour { public float speed; public float stoppingDistance; public float retreatDistance; public GameObject bullet; public Transform player; private float timeBtwShots; public float startTimeBtwShots; // Start is called before the first frame update void Start() { player = GameObject.Find("Player").transform; timeBtwShots = startTimeBtwShots; } // Update is called once per frame void Update() { Vector3 lookVector = player.transform.position - transform.position; lookVector.y = transform.position.y; Quaternion rot = Quaternion.LookRotation(lookVector); transform.rotation = Quaternion.Slerp(transform.rotation, rot, 1); if (Vector3.Distance(transform.position, player.position) > stoppingDistance) { transform.position = Vector3.MoveTowards(transform.position, player.position, speed * Time.deltaTime); } else if (Vector3.Distance(transform.position, player.position) < stoppingDistance && Vector3.Distance(transform.position, player.position) > retreatDistance) { transform.position = this.transform.position; } else if (Vector3.Distance(transform.position, player.position) < retreatDistance) { transform.position = Vector3.MoveTowards(transform.position, player.position, -speed * Time.deltaTime); } if (timeBtwShots < -0) { Instantiate(bullet, transform.position, Quaternion.identity); timeBtwShots = startTimeBtwShots; } else { timeBtwShots -= Time.deltaTime; } } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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