- Home /
Question by
WyrmrestGames · Nov 30, 2018 at 07:47 PM ·
transform.rotationgameobject.tagturrets
2D Rotation to target?
Hey guys, hopefully, you can help me find an answer to what I'm doing wrong. I'm trying to get this turret to automatically reset to new targets and transform its rotation to a new target each time one goes out of range or is destroyed.
So far this is what I've got, I haven't set it up to actually fire bullets yet, but the Rotation works, its just not selecting new and alive targets.
Please note that this is written in C# and not JavaScript...
Thanks in advance to anyone that can help!
Code:
bool isInRange;
GameObject target;
bool hasTarget;
float rotationSpeed = 6.0f;
Rigidbody2D turretRigid;
CircleCollider2D turretCollider;
EnemyStats enemyStats;
GameController gameController;
WaveSpawner waveSpawner;
private void Awake()
{
gameController = GameObject.Find("GameController").GetComponent<GameController>();
waveSpawner = GameObject.Find("WaveSpawner").GetComponent<WaveSpawner>();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(isInRange == true && hasTarget == true && )
{
Invoke("AquireTarget", 0);
transform.Rotate(new Vector3(target.transform.position.x, 0 , -rotationSpeed) * Time.deltaTime);
} else if(isInRange == true && hasTarget == false)
{
CancelInvoke("AquireTarget");
}
}
void AquireTarget()
{
if(hasTarget == true)
{
target = GameObject.FindWithTag("Enemy");
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "Enemy")
{
hasTarget = true;
isInRange = true;
}
}
Comment
Update on the code: bool isInRange;
float range;
public GameObject turretShell;
public GameObject target;
public GameObject theTurret;
bool hasTarget;
Vector2 spawnPosition;
float rotationSpeed = 200.0f;
Rigidbody2D turretRigid;
CircleCollider2D turretCollider;
EnemyStats enemyStats;
GameController gameController;
WaveSpawner waveSpawner;
private void Awake()
{
gameController = GameObject.Find("GameController").GetComponent<GameController>();
waveSpawner = GameObject.Find("WaveSpawner").GetComponent<WaveSpawner>();
theTurret = GameObject.Find("HeavyTurret");
}
// Update is called once per frame
void Update () {
spawnPosition = new Vector3(theTurret.transform.position.x, theTurret.transform.position.y, theTurret.transform.rotation.z + rotationSpeed);
if (waveSpawner.isSpawned == true)
{
enemyStats = GameObject.FindWithTag("Enemy").GetComponent<EnemyStats>();
}
if(isInRange == true && hasTarget == true && waveSpawner.isSpawned == true)
{
transform.Rotate(new Vector3(target.transform.position.x, target.transform.position.y, -rotationSpeed) * Time.deltaTime);
}
if(isInRange == true && hasTarget == true && waveSpawner.isSpawned == true)
{
StartCoroutine(StartFiring());
} else if(isInRange == false && hasTarget == false)
{
StopCoroutine(StartFiring());
}
}
IEnumerator StartFiring()
{
while(hasTarget && isInRange == true)
{
yield return new WaitForSeconds(0.5f);
Instantiate(turretShell, spawnPosition, Quaternion.identity);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "Enemy")
{
target = GameObject.FindWithTag("Enemy");
hasTarget = true;
isInRange = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == "Enemy")
{
hasTarget = false;
isInRange = false;
}
}
I got it to rotate and sort of change targets but the bullets don't transform at the same rotation as the turret.