Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Alternating spawn points, not random

Discussion in 'Scripting' started by firstclassbinch, May 2, 2022.

  1. firstclassbinch

    firstclassbinch

    Joined:
    Feb 28, 2020
    Posts:
    2
    Hello, I have a turret with two barrels. I want it to shoot from one barrel and then the next, and then back to the first. This is my code, I've tried doing different things but I can't figure it out.

    I assumed I could use a boolean and set its value based on the last Shoot method it used, and have the Shoot method be based on what the value of the boolean is. But no matter what it only seems to want to use Shoot2. The first projectile isn't even from Shoot1 despite me setting lastSpawn1 to false.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TurretControl : MonoBehaviour
    6. {
    7.     Transform Player;
    8.     float distance;
    9.     public float maxDistance;
    10.     public Transform turretHead, spawn1, spawn2;
    11.     public GameObject laser;
    12.     public float projectileSpeed = 1000f;
    13.     public float fireRate, nextFire;
    14.     GameObject clone;
    15.     bool lastSpawn1 = false;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         Player = GameObject.FindGameObjectWithTag("Player").transform;
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         distance = Vector3.Distance(Player.position, transform.position);
    27.         if(distance <= maxDistance)
    28.         {
    29.             turretHead.LookAt(Player);
    30.             if(Time.time >= nextFire)
    31.             {
    32.                 nextFire = Time.time + 1f / fireRate;
    33.                 if (lastSpawn1 = false)
    34.                 {
    35.                     Shoot1();
    36.                 }
    37.                 if (lastSpawn1 = true)
    38.                 {
    39.                     Shoot2();
    40.                 }
    41.             }
    42.         }
    43.     }
    44.  
    45.     void Shoot1()
    46.     {
    47.         clone = Instantiate(laser, spawn1.position, turretHead.rotation);
    48.         clone.GetComponent<Rigidbody>().AddForce(turretHead.forward * projectileSpeed);
    49.         lastSpawn1 = true;
    50.     }
    51.  
    52.     void Shoot2()
    53.     {
    54.         clone = Instantiate(laser, spawn2.position, turretHead.rotation);
    55.         clone.GetComponent<Rigidbody>().AddForce(turretHead.forward * projectileSpeed);
    56.         lastSpawn1 = false;
    57.     }
    58. }
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,572
    Well, first of all this line:

    Code (CSharp):
    1.  if (lastSpawn1 = false)
    does not check the lastSpawn1 variable but you set it to false. A single
    =
    is an assignment. Furthermore this if statement would always evaluate to false so it never runs. A similar thing is true for your second if statement but here you set it to true and in turn the if statement would always run.

    You probably want to do something like this:

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         distance = Vector3.Distance(Player.position, transform.position);
    4.         if(distance <= maxDistance)
    5.         {
    6.             turretHead.LookAt(Player);
    7.             if(Time.time >= nextFire)
    8.             {
    9.                 nextFire = Time.time + 1f / fireRate;
    10.                 Vector3 spawnPoint;
    11.                 if (lastSpawn1)
    12.                     spawnPoint = spawn1.position;
    13.                 else
    14.                     spawnPoint = spawn2.position;
    15.                 clone = Instantiate(laser, spawnPoint, turretHead.rotation);
    16.                 clone.GetComponent<Rigidbody>().AddForce(turretHead.forward * projectileSpeed);
    17.                 lastSpawn1 = !lastSpawn1;
    18.             }
    19.         }
    20.     }
     
  3. firstclassbinch

    firstclassbinch

    Joined:
    Feb 28, 2020
    Posts:
    2
    I always do that, = vs == ! Thank you so much, that's exactly what I needed. I really appreciate it
     
    Kurt-Dekker likes this.