- Home /
Unable to Update Multiple GameObjects Together
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;
}
}
}
On which GameObject is the script? Because "On$$anonymous$$ouseEnter" is only called, if the $$anonymous$$ouse is above the GameObject, the script is attached to.
@quest-23
There is a copy of the TowerController.cs script on all 4 towers.
On$$anonymous$$ouseEnter seems to trigger when in range of my Circle Collider 2d. (Which is the effect I'm going for as the towers will target other objects in the given range of their circle collider)
I tested this with 1 tower and the functionality works great, it's when I try and have multiple towers that I run into the issue of only 1 of the towers targeting at a time. Even if the mouse is in the circle collider of more than 1 tower.
Answer by neonblitzer · Jun 03, 2015 at 07:38 PM
The mouse can only be on one object, i.e. only the "topmost" object's OnMouseEnter is called. Its collider blocks the rest. You should probably store the target position (mouse position in this case) somewhere and then have each tower check the distance to it to determine if they are close enough to start tracking it.
That's actually what I was figuring the issue to be, but wasn't sure.
Ultimately I'm not going to have the mouse be the target object, I just wanted it for testing purposes.
I'm going to add in a basic enemy object and verify that multiple towers will react to it's collider.
Thanks for your response!
Answer by Oribow · Jun 03, 2015 at 07:46 PM
I recomend you to use another aproach:
Vector3 ownPosition = transform.position;
ownPosition.y = 0;
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.y = 0;
float distance = Vector3.Distance(ownPosition,mousePos);
if (distance <= range)
{
transform.LookAt(mousePos);
}
This script simply calculate the distance between the tower and the mouse. (Ignoring the height(y)) If the distance is lower then the range of the tower, the tower will start to lookat the mouse.
@quest-23
Thank you so much for this! I'm actually not going to use the mouse long term (as this is going to be a tower defense game). I'm sure I'll use this for something in the future though!
Thanks again!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
[Solved]MouseEnter not working! 1 Answer
How to make a 3D tank turret follow the DIRECTION of mouse cursor? 1 Answer
How do I spawn a prefab where I click? 2 Answers