//My turrets were shooting their targets just fine until I added the SetTarget(); method. I’m not sure what could be going on. Also let me know if there is a better way to upload code to a topic.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Turret : MonoBehaviour
{
public Transform Target;
public Transform RotatingObject;
[SerializeField] private float attackRange = 10;
[SerializeField] private ParticleSystem bulletParticles;
void Start()
{
I’m not sure what the problem is. Are your turrets not shooting anything? Do the enemies have DamageToEnemy components? Only thing I can see that would cause the turrets to not shoot at enemies is if it can’t find any enemies
There is a little issue (which wouldn’t explain your problem)
if(sceneEnemies.Length == 0) { return; }
If you previously had a Target set and there are no more enemies left, this will just return and all the turrets will still try to shoot an enemy that no longer exists. Instead of just returning here, also set Target = null
The turrets shoot at the game object that holds the DamageToEnemy component. If this object is at the base of the enemy, the turrets will shoot at the base of the enemy. Can you show an enemy and where exactly the DamageToEnemy script is located?
The parent object (enemies - holds all enemies) holds a child (enemy - holds scripts and box collider) which holds another child (body - holds the mesh renderer and mesh filter). I have the scripts placed on (enemy) as shown in the lesson.
And for some reason, when using the move tool, the arrows for moving the object are positioned above the object, not on it. Similarly, the distance of how far off the turrets aim is and the distance of how far the arrows are above the game object are the same distance. Is there a way to fix this without moving the child object? I think that may help.
The turrets are going to aim at those arrows. But, that’s the game object’s origin. You can’t move it without moving the game object.
What I would do is to
add an empty child game object to the Enemy and call it something like ‘Target Position’, so the hierarchy would be
Enemy
├ Body
└ Target Position
NOTE You could use the ‘Body’ object instead of adding a new empty game object. This would then mean you can’t move it around 'cos you’ll move the mesh, but it should be the middle of the enemy
Then, either
Create a TargetMarker.cs script and add it to this empty game object. It’s an empty script (remove the boilerplate code) that really just serves as a way to find it.
In the Turret script, replace the FindObjectsOfType<DamageToEnemy> with FindObjectsOfType<TargetMarker>.
Or
Add a field to the DamageToEnemy script called _targetPosition and a ‘getter’ method
[SerializeField] Transform _targetPosition;
public Vector3 GetTargetPosition()
{
return _targetPosition.position;
}
Drag the ‘Target Position’ object into this field in the inspector
In the Turret script, ‘get’ this position and use that instead of the DamageToEnemy script’s position, so you’ll still use FindObjectsOfType<DamageToEnemy> but then you’ll use the getter (and obviously change the code a little because this will give you the Vector3 position instead of a transform)