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

How can I make the enemy shoot at the player

$
0
0
I am trying to get a moving object to shoot at my player but it is just shooting straight forward. // put this with rocket launcher on an enemy so it will shoot at you var attackRange = 30.0; var shootAngleDistance = 10.0; var target : Transform; function Start () { if (target == null && GameObject.FindWithTag("Player")) target = GameObject.FindWithTag("Player").transform; } function Update () { if (target == null) return; if (!CanSeeTarget ()) return; // Rotate towards target var targetPoint = target.position; var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0); // If we are almost rotated towards target - fire one clip of ammo var forward = transform.TransformDirection(Vector3.forward); var targetDir = target.position - transform.position; if (Vector3.Angle(forward, targetDir) < shootAngleDistance) SendMessage("Fire"); } function CanSeeTarget () : boolean { if (Vector3.Distance(transform.position, target.position) > attackRange) return false; var hit : RaycastHit; if (Physics.Linecast (transform.position, target.position, hit)) return hit.transform == target; return false; } //put with sentry gun on object that will shoot at you var projectile : Rigidbody; var initialSpeed = 20.0; var reloadTime = 0.5; var ammoCount = 20; private var lastShot = -10.0; function Update () { // Did the time exceed the reload time? if (Time.time > reloadTime + lastShot && ammoCount > 0) { // create a new projectile, use the same position and rotation as the Launcher. var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation); // Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform. instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed)); // Ignore collisions between the missile and the character controller Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider); lastShot = Time.time; ammoCount--; } } I don't think that the movement script is affecting what I would like to work. Thanks for ant help.

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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