Hey all! Thanks for taking the time to read this.
So, I’m a bit of a noob and I really can’t figure this out. I’m making a tower defense game and I’m trying to instantiate a turret onto an object. it’s working just fine apart from the rotation.
I’d like the turret to be square with the object it’s being placed on and I’m not sure what I’m getting wrong. Any advice would be amazing, thanks in advance.
here’s the script that I’ve added to the “turret base” that I want the turret to be instantiated onto.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Nothing inside the Interactable script which this script inherits from deals with any kind of rotation that could cause a conflict
public class TurretBase : Interactable
{
public GameObject turret;
bool turretBuilt = false;
public TurretItem turretItem;
private Vector3 turretDirection;
private Transform baseTransform;
void Start()
{
//Assign this object's transform to the baseTransform variable
baseTransform = gameObject.transform;
//Assign this object's Y axis rotation to the new Vector3 "turretDirection"
turretDirection = new Vector3(0, gameObject.transform.rotation.y, 0);
}
//This function is called when the player clicks on the tower base
public override void Interact()
{
base.Interact();
SpawnTurret();
}
public void SpawnTurret()
{
// and there is already a turret on this Base...
if (turretBuilt == true)
{
//Access the turret's Turret script and run the UpgradeTurret function
Turret turretScript = turret.GetComponent<Turret>();
turretScript.UpgradeTurret();
turretBuilt = true;
return;
}
//if there isn't a turret on this base...
if (turretBuilt == false)
{
//find out what turret should be built from the build manager script and instantiate it
GameObject turretToBuild = BuildManager.instance.GetTurretToBuild();
turret = Instantiate(turretToBuild, transform.position, this.transform.rotation);
//and set it's material to the default material'
Turret turretScript = turret.GetComponent<Turret>();
turretScript.ChangeTurretMaterial(turret, turretScript.defaultMaterial);
turretBuilt = true;
return;
}
else
{
Debug.LogError("DEVERROR: Something has gone wrong!");
return;
}
}
}
I’ve tried using both the private Vector3 turretDirection and the private Transform baseTransform in the rotation portion of the instantiate line as well as this.transform.rotation and gameObject.transform.rotation.
I’m really stuck here and any ideas at all would be grand. Cheers.