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

Having game objects be parented is making them unmoving.

$
0
0
Hello, I am trying to make a game that is forward moving rail shooter similar to star fox. The setup I am trying to have is the parent game object that hold the player character in it and the enemies that become instantiated will also become a child of the parent game object once within a certain distance. The problem I am facing is that when I instantiate enemies finally becomes a child of the parent game object it stops moving even though I have scripts that would have moved the enemy game objects towards the player if they weren't stuck. I hope I made some sense and could really use some help with this. Any advice is super appreciated. Here is the script that the enemy uses before being a child to another game object: using UnityEngine; using System.Collections; public class EnemySpawnBehavior : MonoBehaviour { public float targetDistance = 20.0f; public float enemySpeed = 40.0f; private bool spawningIn = true; // Update is called once per frame void Update () { SpawnIn (); } void SpawnIn() { if (spawningIn == true) { GameObject plane = GameObject.FindGameObjectWithTag("PlayerPlane"); if (transform.position.z - plane.transform.position.z <= targetDistance) { Debug.Log ("Made it to the playing field"); SendMessage("EnemyMovingAnimation", false); transform.parent = plane.transform; SendMessage ("BeginTheAttack", true); spawningIn = false; } else { SendMessage("EnemyMovingAnimation", true); transform.position += transform.forward*enemySpeed*Time.deltaTime; } } } } And here is the script that would've been used if the enemy was able to chase the player: using UnityEngine; using System.Collections; public class EnemyAIMelee : MonoBehaviour { private bool AttackStatus; public float attackDistance = 10.0f; public float chaseSpeed = 1.0f; private bool unlockAI; public GameObject playerCharacterManager; // Use this for initialization void Start() { playerCharacterManager = GameObject.FindGameObjectWithTag ("PlayerCharacterManager"); AttackStatus = false; } // Update is called once per frame void Update () { if (AttackStatus == true) { MovingAI (); } } void MovingAI() { unlockAI =true; if(unlockAI == true) { chaseSpeed *= Time.deltaTime; transform.localRotation = Quaternion.LookRotation(transform.position - playerCharacterManager.transform.position); transform.localPosition += transform.forward*chaseSpeed*Time.deltaTime; Debug.Log ("MovingAI has been reached"); SendMessage ("EnemyMovingAnimation", true); float distance = Vector3.Distance(playerCharacterManager.transform.position, transform.position); if(distance<=attackDistance) { AttackAI(); } } } void AttackAI() { unlockAI =false; SendMessage ("EnemyAttackAnimation", true); Debug.Log ("AttackAI has been reached"); WaitTime(); } void WaitTime() { SendMessage ("EnemyAttackAnimation", false); Invoke ("MovingAI",2.0f); } void BeginTheAttack(bool chase) { AttackStatus = chase; } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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