- Home /
Question by
Agent27765 · Oct 23, 2017 at 11:51 PM ·
c#scripting problem
Building Placement Script Places Item When Switching Buildings.
Hello . Recently I have been attempting to implement a turret system into my Top Down Shooter. the problem I have with this though is that I have 3 different types of turrets in which I choose from. When I choose a certain turret like the first turret from the button it will work. After if I didn't place my object and choose to switch the turret when I click the other button the previous building will still be placed first then the new item will become the option to place. I would really like it if someone can help me with this! Thank you!
ClickPlaceDrop Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointClickPlace : MonoBehaviour {
public Transform currentTurret;
public Camera MainCamera;
private OpenBulletChooser TurretChooser;
public bool TurretChoosen;
public int TypeTurretChoosen;
// Use this for initialization
void Start () {
TurretChooser = GetComponent<OpenBulletChooser>();
}
// Update is called once per frame
void Update () {
if (TurretChoosen == true)
{
Vector3 mPosition = Input.mousePosition;
mPosition = new Vector3(mPosition.x, mPosition.y, transform.position.y);
Vector3 Camera = MainCamera.ScreenToWorldPoint(mPosition);
currentTurret.position = new Vector3(Camera.x, 1, Camera.z);
if (Input.GetMouseButtonDown(1) && TurretChoosen == true)
{
if (TypeTurretChoosen == 0)
{
Instantiate(currentTurret, new Vector3(Camera.x, 1, Camera.z), currentTurret.rotation);
TurretChoosen = false;
}
else if(TypeTurretChoosen == 1)
{
Instantiate(currentTurret, new Vector3(Camera.x, 1, Camera.z), currentTurret.rotation);
TurretChoosen = false;
}
else if(TypeTurretChoosen == 2)
{
Instantiate(currentTurret, new Vector3(Camera.x, 1, Camera.z), currentTurret.rotation);
TurretChoosen = false;
}
}
}
}
public void SetTurret(GameObject Turret)
{
currentTurret = ((GameObject)Instantiate(Turret)).transform;
TurretChoosen = true;
}
}
Part Of Button Selection Script
//Choose Types Of Turrets
public void FirstTurret()
{
ClickPlaceScript.SetTurret(TypesOfTurrets[0]);
}
public void SecondTurret()
{
ClickPlaceScript.SetTurret(TypesOfTurrets[1]);
}
public void ThirdTurret()
{
ClickPlaceScript.SetTurret(TypesOfTurrets[2]);
}
}
Comment