Laser turret offset problem (Tower Defense Game)

Updated on June 28, 2017 in [A] Brackeys Courses
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on June 14, 2017

There’s a problem with the offsetting of the laser turret when it spawns on a selected node. Currently, for all nodes the turrets are to be spawned at a .5 offset on the y-axis. However, when it comes to the laser turret being selected on a node it ends up hovering in mid-air. I assume brackeys did not catch this problem because he viewed the turret’s spawning from a top-down view, or he didn’t have this problem at all. Is there a way of only affecting the offset of the laser turret alone, aside from the others? Any help is appreciated, as I did make attempts of doing so. Here’s the following code to clarify:

Node script:

public class Node : MonoBehaviour {
public Color hoverColor;
 public Color notEnoughMoneyColor;
 public Vector3 positionOffset;
[Header("Optional")]
 public GameObject turret;
private Renderer rend;
 private Color startColor;
BuildManager buildManager;
void Start()
 {
 rend = GetComponent<Renderer>();
 startColor = rend.material.color;
buildManager = BuildManager.instance;
 }
public Vector3 GetBuildPosition()
 {
 return transform.position + positionOffset;
 }
void OnMouseDown()
 {
 if (EventSystem.current.IsPointerOverGameObject())
 return;
if (!buildManager.CanBuild)
 return;
if (turret != null)
 {
 return;
 }
buildManager.BuildTurretOn(this);
}
void OnMouseEnter()
 {
 if (EventSystem.current.IsPointerOverGameObject())
 return;
if (!buildManager.CanBuild)
 return;
if (buildManager.HasMoney)
 {
 rend.material.color = hoverColor;
 }
 else
 {
 rend.material.color = notEnoughMoneyColor;
 }
 }

Build Manager script:

public class BuildManager : MonoBehaviour {
public static BuildManager instance;
void Awake ()
 {
 if (instance != null)
 {
 Debug.LogError("More than one Build Manager!");
 return;
 }
instance = this;
 }
public GameObject buildEffect;
private TurretBlueprint turretToBuild;
public bool CanBuild { get { return turretToBuild != null; } }
 public bool HasMoney { get { return PlayerStat.Money >= turretToBuild.cost; } }
public void BuildTurretOn(Node node)
 {
 if(PlayerStat.Money < turretToBuild.cost)
 {
 Debug.Log("Not enough money to build turret!");
 return;
 }
PlayerStat.Money -= turretToBuild.cost;
GameObject turret = Instantiate(turretToBuild.prefab, node.GetBuildPosition(), Quaternion.identity);
 node.turret = turret;
GameObject effect = Instantiate(buildEffect, node.GetBuildPosition(), Quaternion.identity);
 Destroy(effect, 5f);
Debug.Log("Turret built! Money left: " + PlayerStat.Money);
 }
public void SelectTurretToBuild(TurretBlueprint turret)
 {
 turretToBuild = turret;
 }
}
  • Liked by
Reply
0 on June 14, 2017

change the public vector3 positionOffset..

  • Liked by
Reply
Cancel
0 on June 28, 2017

if i were to do that, it would also affect the other turret placements too, as i already tried.

Don’t worry though. Found the solution already in one of his later vids.

  • Liked by
Reply
Cancel