Quantcast
Channel: Latest Questions by Fox-Handler
Viewing all articles
Browse latest Browse all 90

Target lost once player is instantiated

$
0
0
I have a character that is reinstantiated whenever he gets attacked by turrets, but once he dies and I respawn him, the turret loses its target and they just stay motionless. How could I set the character as a target so that once he dies and is respawned, the turrets are still able to track him? About the closest i've gotten is finding the gameObject once the character is destroyed/null, but I still get an error. using UnityEngine; using System.Collections; public class Turret : MonoBehaviour { public TurretGun turretGun; private float distance; private Quaternion rotation; private float lookAtDistance = 10; private float reactionTime = 3; private Quaternion originalRotation; private float attackRange = 10; public Transform target; void Start() { target = GameObject.FindGameObjectWithTag("Player").transform; } void Update () { if(target) { distance = Vector3.Distance(target.position, transform.position); if (distance < lookAtDistance) { lookAt(); } if (distance > lookAtDistance) { originalRotation = Quaternion.identity; } if (distance < attackRange) { attack(); } } else { target = null; } if (target == null) { Target (); } } public void Target() { target = GameObject.FindGameObjectWithTag("Player").transform; } void lookAt () { rotation = Quaternion.LookRotation(target.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * reactionTime); } void attack() { turretGun.ShootContinuous(); } }

Viewing all articles
Browse latest Browse all 90

Trending Articles



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