- Home /
Tower Defense tpwers follow player untill i klick
hi there! I'm into a Tower Defens games now and encountered a problem I do not have a clue how to solve: (. In my game running around in fps mode and where I press the mouse now "build" my tower. But I want that when I chose my tower as the player must see the tower on the ground 2-3 meters in front of him and it shall be transparant. and then when I press the mouse so must the tower be built on that spot that the player sees the transparent tower. Here is the code I have now
chosing tower using UnityEngine; using System.Collections;
public class väljtornvidklick : MonoBehaviour {
public GameObject towerSelector;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void Clicked()
{
// beräta för tornväljaren att ändra valt torn
towerSelector.SendMessage ("SetSelectedTower", gameObject);
}
}
and here is the script for changing the tower enter code hereusing UnityEngine; using System.Collections;
public class tornväljaren : MonoBehaviour {
public GameObject[] towersIcons;
public GameObject[] towers;
public int[] towersCosts;
public float towerIconRotaionRate = 01.0f;
private int selectedTower = 0;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
towersIcons[selectedTower].transform.Rotate(Vector3.up, towerIconRotaionRate * Time.deltaTime);
}
public GameObject getSelectedTower()
{
return towers[selectedTower];
}
public int GetSelectedTowerCost()
{
return towersCosts [selectedTower];
}
void SetSelectedTower(GameObject inputTower)
{
int index = 0;
foreach (GameObject towerIcon in towersIcons)
{
if (inputTower == towerIcon)
{
selectedTower = index;
}
index++;
}
}
}
And finaly the script for building the towers using UnityEngine; using System.Collections;
public class ByggTornVidKlick : MonoBehaviour {
public tornväljaren towerSelector;
private Vector3 temp;
public Camera mainCamera;
void Clicked(Vector3 position)
{
if (!Input.GetButton ("Ctrl"))
{
if (EnergyManager.energy >= towerSelector.GetSelectedTowerCost ()) {
GameObject tower = towerSelector.getSelectedTower ();
Instantiate (tower, position + Vector3.up * 0.5f, tower.transform.rotation);
EnergyManager.energy -= towerSelector.GetSelectedTowerCost ();
}
}
}
void Update ()
{
}
}
is there anyone who can help me?
I don't see a clear question here. Are you asking someone to code a solution for you? That's not what UA is all about. UA is best at solving single, specific technical issue.
aa i am sorry! i do not whant someone to do the jobb for me i whanted a push so i can start. dident have any idea on how to solv the problem
Answer by sparkzbarca · Jan 02, 2014 at 03:27 AM
ill get ya started
//first click shows the transparent object, second builds it, if you click once dobuild
//becomes true
bool DoBuild;
public GameObject Tower;
void Start()
{
DoBuild = false;
}
Void Update()
{
if(input.getbuttondown("Build Tower")
{
if(!DoBuild)
{
DoBuild = true;
ShowTransparent();
}
else
{
Build();
Dobuild = false;
}
}
void ChangeAlpha(GameObject Piece, float Alpha)
{
Color TempColor = Piece.renderer.material.color;
TempColor.a = Alpha;
Piece.renderer.material.SetColor("_Color", TempColor);
}
GameObject Transparent;
void ShowTransparent()
{
Transparent = Instantiate(Tower, player.transform.position + (2 * player.transform.forward), quanternion.identity);
ChangeAlpha(Transparent, .5f);
Transparent.transform.parent = player.transform;
}
void Build()
{
ChangeAlpha(Transparent, 1f);
Transparent.transform.parent = null;
}
this still needs ALOT
it needs the abillity to rotate the tower, needs a collision check to make sure it can be built where it is.
it spawns directly in front of the player without regard for the terrain height that needs solved.
It has many issues but its a good start.
aa thx this is whant i needed :). now i can start to work and change it so i can use it to my game and terrain :)
Your answer
Follow this Question
Related Questions
An another problem - Tower Defense GUI ver.2. 1 Answer
What is the procedure of spawning enemies in each round ? TD games 1 Answer
Tower defense blender grated tower get wird when i use destroy 1 Answer
How to implement fast forward feature in a tower defense game? 1 Answer
My tower defense script fires too often 5 Answers