Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Struggling with a dynamic spawner (rotating)

Discussion in 'Scripting' started by Pwnzrd, Mar 15, 2019.

  1. Pwnzrd

    Pwnzrd

    Joined:
    Sep 24, 2015
    Posts:
    5
    Hey guys I have made a rotating spawner on an empty object that spins around the player and switches direction, the zombies are in fog so they seem to randomly come in from angles, what I really cant figure out is how to ensure they never spawn behind as my AI isn't great it needs a direct path, this way I could add things like houses to hold out in! Thank you!!!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RotateAroundObject : MonoBehaviour
    5. {
    6.     public GameObject target;
    7.     public bool flipDirection = false;
    8.     public float rotatespeed = 10;
    9.     public float switchspeed = 10;
    10.  
    11.     public void Start()
    12.  
    13.     {
    14.  
    15.         DirectionSwitch();
    16.  
    17.     }
    18.  
    19.  
    20.  
    21.     void Update ()
    22.     {
    23.  
    24.         StartCoroutine(DirectionSwitch());
    25.  
    26.         Vector3 direction = flipDirection ? -target.transform.up : target.transform.up;
    27.  
    28.         transform.RotateAround(target.transform.position, direction, (rotatespeed * Time.deltaTime));
    29.  
    30.  
    31.     }
    32.  
    33.  
    34.  
    35.     IEnumerator DirectionSwitch()
    36.  
    37.     {
    38.  
    39.  
    40.  
    41.         if (flipDirection == false)
    42.         {
    43.             yield return new WaitForSeconds(switchspeed);
    44.  
    45.             flipDirection = true;
    46.  
    47.         }
    48.  
    49.         else if (flipDirection == true)
    50.         {
    51.             yield return new WaitForSeconds(switchspeed);
    52.  
    53.             flipDirection = false;
    54.  
    55.         }
    56.        
    57.  
    58.     }
    59.  
    60.     IEnumerator Pause()
    61.     {
    62.  
    63.        yield return new WaitForSeconds(12f);
    64.  
    65.     }
    66.  
    67.  
    68. }
    69.  
    :: unity 2018.2.20f1
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    You're trying to make sure they never spawn behind what?

    Also, it's worth mentioning that NavMesh is pretty easy to use, and it's pretty good for simple pathing. You might consider addressing the issue indirectly by improving your AI's pathfinding, rather than trying to work around it.
     
  3. Pwnzrd

    Pwnzrd

    Joined:
    Sep 24, 2015
    Posts:
    5
    Behind the front facing angle of the character, otherwise the enemies will be stuck behind a wall and i cant get them to use navmesh correctly atm, its "hack" for now to ensure good gameplay, I am a noob :) Thanks for the reply

    at the moment you just walk through fog but i thought adding in some places to "hold out" would be cool but the spawner needs to be sure that it can err see me but not past a wall, it keeps rotating so could just check on update() if it will be able to make the way to me

    to simply: pathfinder bad, I am bad
    I just want the rotating spawn-er to not spawn if it cant see a route to me without wall blocking it, i know it sounds a bit strange but I am in early alpha, it works fine wout that feature but hiding places are cool :D
     
    Last edited: Mar 15, 2019
  4. Pwnzrd

    Pwnzrd

    Joined:
    Sep 24, 2015
    Posts:
    5
    at the moment the (random direction switching) spinner with a timed spawner that checks if the clone is dead works fine I just thinking about more features as it's just running in fog at would be cool to have random hold off points (image very from old version, hair, breasts, and the (new) skirt have bones that work with physics aswell. It also has a dynamic day / night cycle already too
     

    Attached Files:

  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    If you really just need to make sure line-of-sight to the player is possible from a particular location, then look into using Physics.Raycast to check whether there are any obstacles between the spawner and the player. You can choose which kinds of objects should be considered obstructions (like you player) and which might not obstruct the line of sight (like, for example, a campfire).
     
  6. Pwnzrd

    Pwnzrd

    Joined:
    Sep 24, 2015
    Posts:
    5
    I think you are completely correct I am very beginner with raycast, just enough for the shooting system, could you please add some code to that spawn er so it can detect the player and if it cant dont spawn
     
  7. Pwnzrd

    Pwnzrd

    Joined:
    Sep 24, 2015
    Posts:
    5
    but it must only matter if there is an obstruction
     
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193