It’s an rts game, whenever I right click I instantiate an object to indicate the target location the player is moving to. The newly instantiated object does a very short animation for about 0.5 seconds and then destroys itself.
It’s running smoothly unless I spam right click on the same location. This means that I am now actually clicking on the “target indicator object”, because I click more than once every 0.5sec which is the animation time before object is destroyed.
This causes the indicator object to seem like it’s growing bigger and bigger for each mouse click, until it doesn’t fit the screen anymore. Then it becomes normal size and starts all over again from normal size.
In the scene view it looks more like it’s moving closer and closer to the camera for each click, until it is out of camera sight.
So how do I fix this problem?
This is where I instantiate the object, I don’t believe this is where the mistake is, but take a look please:
if(Input.GetMouseButtonDown(1)) {
GameObject newPosition = GameObject.Find("RightClickTarget");
newPosition.transform.position = hit.point; // hit = RaycastHit
DestroyObject(GameObject.Find("Target Instantiated")); // If indicator already exists, delete the last one
GameObject targetObj = Instantiate(target, hit.point, Quaternion.identity) as GameObject; // This is the indicator object
targetObj.name = "Target Instantiated";
}
Maybe it is because of the animation? When the target is instantiated, it moves a few steps up in the Y-direction, then sinks down below the terrain and destroys itself.
Or is it because I don’t use layermasks? Don’t know much about how to use them, but maybe I can put the instantiated object in a layer that is ignored by the
RaycastHit hit ?