Hello!
I’m new to Unity, but have some experience with C#.
In order to familiarize myself with Unity I’ve decided to create a simple top-down tower defense game.
Currently I’m working on the TowerController script that will allow the towers to pivot and follow enemies as they enter a given radius. I’ve figured out how to handle the triggering within a radius and have the tower rotate and face a target, however, when I have multiple towers placed in the scene they don’t respond at the same time. Currently I’m using the MousePosition as the the target for the towers. In the future I will be using other gameObjects. It appears that the towers cannot trigger at the same time. I’m not sure why this is and any feedback would be greatly appreciated.
Below is a screenshot of the scene:
Here is a screenshot of the tower GameObject:
Finally here is the TowerController.cs script that I’m using:
using UnityEngine;
using System.Collections;
public class TowerManager : MonoBehaviour {
private bool _tracking = false;
void OnMouseEnter() {
_tracking = true;
Debug.Log (gameObject.name + " tracking mouse");
}
void OnMouseExit() {
_tracking = false;
}
void Update () {
if (_tracking) {
var mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Quaternion rot = Quaternion.LookRotation (transform.position - mousePos, Vector3.forward);
gameObject.transform.rotation = rot;
}
}
}