Search Unity

Games Moses Exodus

Discussion in 'Works In Progress - Archive' started by starmind001, Jan 7, 2019.

  1. starmind001

    starmind001

    Joined:
    Jul 20, 2010
    Posts:
    95
    Moses: Exodus

    I have made some games in the past, some were completed while others still sit with no promise of going forward. My wife suggested that I make a bible game. I informed her that most bible games suck due to the constant quizzes and bad music. I didn't want to make one. I made her a deal, if I started the game and it looks promising, then I would continue in my spare time. But the music, quizzes and (most important) the gameplay must all work. She has agreed with my demands.
    So, I began this little game. My unity project file became known as Project Moses. After a few days of tinkering with code and other ideas, I started a new project file called Exodus.
    Now I did my research for level design and character models, even whether it would be realistic or low poly. I even went and watched youtube (most notably AVGN) and read articles of what bible games were worthy of playing and what was not. I found that only 2 bible games have ever been considered worthy. Those games were Spiritual Warfare(NES) and Left Behind(PC). One is a Zelda clone and the other is a simple RTS.
    My problem is how do you make a game currently without some element being a clone of something. So I did what I could to avoid cloning other games, but the more I make this game, I am noticing elements of other games. Will some things change as I go? Most definitely. In fact, if you watch the videos, you will see some changes.
    Please make comments and ideas or even the dreaded crit.
    Enjoy!

    Day 1 Build:


    Day 2 Build:



    Day 3 Build:


    Day 4 Build:


    Day 5 Build:
     
    GarBenjamin likes this.
  2. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    788
    Every game is going to be similar to some other game, there's no way to avoid that, and just because bible games were bad in the past doesn't mean yours has to be. In fact, it's a good thing since yours could be one of the few good ones!

    I like the aesthetics, thought the ground and grass looks a bit inconsistent with the other objects in your final video.

    It looks quite difficult and finicky to aim the attacks, are you thinking of adding lock on targetting or a crosshair of some sort?

    Also what platforms are you intending to release on?

    One more thing, don't dread critisism, getting honest criticism is one of the most valuable things when developing a game. It's difficult to see your games downfalls when you've gotten use to them and adjusted for them. Fresh eyes will see the issues in your game and you need to embrace those opinions early on rather than showing everyone your completed project and being overwhelmed with the amount of critiques changes that are requested.

    Keep it up! It's looking great!
     
    starmind001 likes this.
  3. starmind001

    starmind001

    Joined:
    Jul 20, 2010
    Posts:
    95
    Thank you Serinx!
    Yes, the ground textures are just place holders at this time. Pretty much all of the models are place holders with the exception of the character models. This level is just a setup level. I just got tired of seeing a gray texture and began adding textures as I needed for an idea for an area and design.

    I have though about a target lock, but I have yet to implement it.

    My plan is to release for the Xbox One, PC, web and possibly android. Xbox and PC for sure.
     
    Serinx likes this.
  4. starmind001

    starmind001

    Joined:
    Jul 20, 2010
    Posts:
    95
    Small update!
    Added animal AI. Basically they just wander around within a certain area.
    Added the ability to pickup and carry objects when player is close to them.

    Currently having a problem with the script for carrying. I want to enable the particle system, but it doesn't want to. I am asking for a little help on that one here.
    https://forum.unity.com/threads/particle-system-enable-when-carrying.609187/

    Day 6 Build:
     
    Serinx likes this.
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    It's probably impossible. I don't think anyone's ever done this before.
     
    Serinx and starmind001 like this.
  6. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    788
  7. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    That's just a clone of actual ping-pong. ;)
     
    starmind001 and Serinx like this.
  8. starmind001

    starmind001

    Joined:
    Jul 20, 2010
    Posts:
    95
    I had a little time last night to work on this. I tried to make some improvements in certain areas, but it caused another issue I wasn't able to fix last night.

    Updates:
    • Added a chicken model with animations. This seems to work okay, but sometimes has an issue when trying to drop. You can see in the video the issues.
    • Added a sheep model that roams around. The script needs some work as I would like to edit random animations for sleeping, grazing and other animations.
    • Fixed issues with chicken script, where the feathers turn on and off. Also fixed input.getbutton to work while using the script. This caused an interesting issue later. I will need to fix this. I think I need to add a function that checks where the chicken is so the problem doesn't happen again. Tried to make a function to throw the chicken, but that was a fail. Might come down to another approach on how to do the same idea. I will attach the code at the bottom of this post if someone might see an alternative solution.
    • Started an idea for a quest template. That old find my chickens and put them where I say idea.
    • They are not seen, but made quite a few NPCs with interchangeable textures and animations, along with other animals that will be needed in the game. Also created a few Easter eggs to be shown in the game. I know that it is not needed, but I wanted them. To me it added something to the game. Nothing like GTA:VC Easter egg.
    • Something else I added, but doesn't seem to work well is a targeting system. It was suggested and I tried to add it, but it was a fail. I will have to look into a new system. I would like to have one similar to Zelda 64(clone), but an unique one. I made one in the past, but it didn't translate to well into unity 2017 very well. Might just be a coding error or something else.
    Pretty much all I have done in this day build. I think alot of the problems I am having is trying to make the whole game work with the xbox controller. I do wish to make this an xbox game so I am trying to give it better controls than the PC. PC controls work great, but I always run into a little lag or input problem with the controller. For now everything I have done is working okay and is functioning except for FirstSelected when using UI.
    Enjoy the videos!

    Day 7 Build:


    Day 8 Build:


    Grab and Carry Code:
    Code (CSharp):
    1. public class GrabCarryObject : MonoBehaviour
    2. {
    3.     public GameObject item;
    4.     public GameObject tempParent;
    5.     public Transform guide;
    6.     public float range;
    7.     public float speed;
    8.     public bool carrying;
    9.     NavMeshAgent agent;
    10.     AnimalAI animalAI;
    11.  
    12.     private void OnTriggerEnter(Collider other)
    13.     {
    14.         if(other.gameObject.CompareTag("AnimalPickUp"))
    15.         {
    16.             item = other.gameObject;
    17.             item.GetComponent<Rigidbody>().useGravity = true;
    18.             agent = item.GetComponent<NavMeshAgent>();
    19.             item.GetComponent<AnimalAI>();
    20.         }
    21.  
    22.     }
    23.  
    24.     void Update ()
    25.     {
    26.         if(carrying == false)
    27.         {
    28.             if(Input.GetButtonDown("Fire1") && (guide.transform.position - transform.position).sqrMagnitude < range * range)
    29.             {
    30.                 Pickup();
    31.                 carrying = true;
    32.             }
    33.         }
    34.  
    35.         else if(carrying == true)
    36.         {
    37.             if(Input.GetButtonUp("Fire1"))
    38.             {
    39.                 Drop();
    40.                 carrying = false;
    41.             }
    42.         }
    43.     }
    44.  
    45.     void Pickup()
    46.     {
    47.         if (item == null)
    48.             return;
    49.         else
    50.         {
    51.             item.GetComponent<AnimalAI>().FeathersOn();
    52.             item.GetComponent<Rigidbody>().useGravity = false;
    53.             item.GetComponent<Rigidbody>().isKinematic = true;
    54.             item.GetComponent<Collider>().enabled = false;
    55.             agent.enabled = false;
    56.             item.transform.position = tempParent.transform.position;
    57.             item.transform.rotation = tempParent.transform.rotation;
    58.             item.transform.parent = tempParent.transform;
    59.         }
    60.     }
    61.  
    62.     void Drop()
    63.     {
    64.         if (item == null)
    65.             return;
    66.         else
    67.         {
    68.             item.GetComponent<Rigidbody>().useGravity = true;
    69.             item.GetComponent<Rigidbody>().isKinematic = false;
    70.             item.GetComponent<Collider>().enabled = true;
    71.             agent.enabled = true;
    72.             item.transform.parent = null;
    73.             item.transform.position = tempParent.transform.position;
    74.             //item.GetComponent<AnimalAI>().FeathersOff();
    75.         }
    76.     }
    77.  
    78.     private void OnTriggerExit(Collider other)
    79.     {
    80.         if (other.gameObject.CompareTag("AnimalPickUp"))
    81.         {
    82.             item.GetComponent<AnimalAI>().FeathersOff();
    83.             item = null;
    84.         }
    85.         else
    86.         {
    87.             if (other.gameObject.CompareTag("AnimalPickUp"))
    88.             {
    89.                 item = other.gameObject;
    90.                 item.GetComponent<Rigidbody>().useGravity = true;
    91.             }
    92.         }
    93.     }
    94. }
    Chicken Script:
    Code (CSharp):
    1. public class AnimalAI : MonoBehaviour
    2. {
    3.     public float radius;
    4.     public float timer;
    5.     public float Wait;
    6.     public GameObject fx;
    7.  
    8.     private Transform target;
    9.     private NavMeshAgent agent;
    10.     private float currentTimer;
    11.  
    12.     public bool idle;
    13.     public bool Flap;
    14.     public float idleTimer;
    15.     private float currentIdleTimer;
    16.  
    17.  
    18.     public Animator anim;
    19.  
    20.     void OnEnable()
    21.     {
    22.         agent = GetComponent<NavMeshAgent>();
    23.         anim = GetComponent<Animator>();
    24.         fx.SetActive(false);
    25.         currentTimer = timer;
    26.         currentIdleTimer = idleTimer;
    27.     }
    28.  
    29.     private void Update()
    30.     {
    31.         anim.SetBool("isWalking", false);
    32.         currentTimer += Time.deltaTime;
    33.         currentIdleTimer += Time.deltaTime;
    34.         if(currentIdleTimer >= idleTimer)
    35.         {
    36.             StartCoroutine("switchIdle");
    37.         }
    38.  
    39.         if(currentTimer >= timer && !idle && agent.isActiveAndEnabled)
    40.         {
    41.             Vector3 newPosition = RandomNavSphere(transform.position, radius, -1);
    42.             agent.SetDestination(newPosition);
    43.             currentTimer = 0;
    44.         }
    45.  
    46.         if(idle)
    47.         {
    48.             anim.SetBool("isWalking", false);
    49.             anim.SetBool("Flapper", false);
    50.             anim.SetBool("isIdle", true);
    51.         }
    52.         if(Flap)
    53.         {
    54.             anim.SetBool("isWalking", false);
    55.             anim.SetBool("isIdle", false);
    56.             anim.SetBool("Flapper", true);
    57.         }
    58.         else
    59.         {
    60.             anim.SetBool("isWalking", true);
    61.             anim.SetBool("isIdle", false);
    62.         }
    63.     }
    64.  
    65.     IEnumerator switchIdle()
    66.     {
    67.         idle = true;
    68.         agent.velocity = Vector3.zero;
    69.         yield return new WaitForSeconds(Wait);
    70.         currentIdleTimer = 0;
    71.         idle = false;
    72.     }
    73.  
    74.     public static Vector3 RandomNavSphere(Vector3 origin, float distance, int layerMask)
    75.     {
    76.         Vector3 randomDirection = Random.insideUnitSphere * distance;
    77.         randomDirection += origin;
    78.  
    79.         NavMeshHit navHit;
    80.         NavMesh.SamplePosition(randomDirection, out navHit, distance, layerMask);
    81.  
    82.         return navHit.position;
    83.     }
    84.    
    85.     public void Stop()
    86.     {
    87.         agent.velocity = Vector3.zero;
    88.     }
    89.  
    90.     public void Resume()
    91.     {
    92.         currentIdleTimer = 0;
    93.         StartCoroutine("switchIdle");
    94.     }
    95.  
    96.     private void OnDrawGizmosSelected()
    97.     {
    98.         Gizmos.color = Color.green;
    99.         Gizmos.DrawWireSphere(transform.position, radius);
    100.     }
    101.  
    102.     public void FeathersOn()
    103.     {
    104.         fx.SetActive(true);
    105.         Flap = true;
    106.     }
    107.     public void FeathersOff()
    108.     {
    109.         Flap = false;
    110.         fx.SetActive(false);
    111.     }
    112. }
     
  9. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    One problem I see is that in your "drop" code you are changing the values for "agent" and "item" so that these are no longer attached to your character- so you are assuming that "agent" and "item" are pointing to the specific object that you are carrying. This may not be the case, though, because you have an onTriggerEnter code which is assigning a new value to "item" and "agent". That means the item you are trying to drop may not even be the item that you are carrying. It could just be a different item that you walked close to.
     
  10. starmind001

    starmind001

    Joined:
    Jul 20, 2010
    Posts:
    95
    I see some of my problem. How do I check if another game object with the same tag? Basically the trigger is there to identify the gameobject and then allow the interaction, but when two with the tag are to close, the item returns null. I am not seeing how to remove an item that has been removed because of the tag check is not there.
     
  11. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    THe most obvious fix to the problem that I mentioned is to replace "item" with two variables: One for the item you are holding (set it only when you pick-up an item), and another one that you use for the onTriggerEnter.

    As for the nav mesh agent: there is no need to have a reference to it until you actually pickup the item (where you disable the agent). Simply remove the assignment of "agent" from the onTriggerEnter and put it in the Pick-up code instead.
     
  12. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    I'm not exactly sure why that would be, except it's probably something to do with the way that you are using onTriggerEnter and onTriggerExit.
    One thing to consider: What if you have two chickens that you can pick up (we'll call chicken A and chicken B) and the player wants to walk past chicken A and pick up chicken B. you first get close to chicken A and it triggers onTriggerEnter, setting "item" to chicken A. Then you inch on over to chicken B, which triggers onTriggerEnter, setting "item" to chicken B. Now chicken A and chicken B are both within the players trigger collider, but the player is still trying to get closer to chicken B and is walking away from chicken A. Walking away from chicken A triggers the onTriggerExit event setting "item" to null. Now the player is standing right next to chicken B, but he can't pick-up the chicken because "item" is set to null.
     
  13. starmind001

    starmind001

    Joined:
    Jul 20, 2010
    Posts:
    95
    Yes that would be my issue. Trying to set the holding, but it is not working like it needs to. Might be a dumb question here, but how do I set the holding in the pickup?
     
  14. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    I'm not supposing that this is the best way, necessarily, but here is an alternative way to implement grab-and-carry that might simplify things (back up your original code in case this doesn't work):

    • get rid of the onTriggerEnter and onTriggerExit events entirely.
    • In your Update, check for
      if(Input.GetButtonUp("Fire1"))
      like you are doing now, but when the player presses the button use something like Physics.OverlapSphere, using your guide and range variables for the sphere parameters. This will give you an array of all the objects with colliders that are in that sphere.
    • loop through the array and simply set "item" to the first object that has the right tag and call your pick-up method. Break the loop upon returning so that you don't pick-up any more objects.

    Using the overlap sphere, you can check collisions instantaneously, without having to kepp track of what's coming in to range and what's leaving.
     
  15. starmind001

    starmind001

    Joined:
    Jul 20, 2010
    Posts:
    95
    I was able to fix the script from leaving the chickens in mid air. Using the overlapSphere I was able to get the chickens to do what I needed. Now to the next issues.

    Final Carry code:
    Code (CSharp):
    1.  public GameObject item;
    2.     public GameObject tempParent;
    3.     public Transform guide;
    4.     public float range;
    5.     public float radius;
    6.     public bool carrying;
    7.     NavMeshAgent agent;
    8.     AnimalAI animalAI;
    9.  
    10.     void Update ()
    11.     {
    12.         if (carrying == false)
    13.         {
    14.             if (Input.GetButton("Fire1") && (guide.transform.position - transform.position).sqrMagnitude < range * range)
    15.             {
    16.                 Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
    17.                 foreach (Collider nearbyObject in colliders)
    18.                 {
    19.                     if(nearbyObject.gameObject.tag == "AnimalPickUp")
    20.                     {
    21.                         item = nearbyObject.gameObject;
    22.                         Pickup();
    23.                         carrying = true;
    24.                     }
    25.  
    26.                 }
    27.             }
    28.         }
    29.  
    30.         else if (carrying == true)
    31.         {
    32.             if (Input.GetButton("Fire3"))
    33.             {
    34.                 Drop();
    35.                 carrying = false;
    36.             }
    37.         }
    38.     }
    39.  
    40.     void Pickup()
    41.     {
    42.         if (item == null)
    43.             return;
    44.         else
    45.         {
    46.             agent = item.GetComponent<NavMeshAgent>();
    47.             item.GetComponent<AnimalAI>();
    48.             item.GetComponent<AnimalAI>().FeathersOn();
    49.             item.GetComponent<Rigidbody>().useGravity = false;
    50.             item.GetComponent<Rigidbody>().isKinematic = true;
    51.             item.GetComponent<Collider>().enabled = false;
    52.             agent.enabled = false;
    53.             item.transform.position = tempParent.transform.position;
    54.             item.transform.rotation = tempParent.transform.rotation;
    55.             item.transform.parent = tempParent.transform;
    56.         }
    57.     }
    58.  
    59.     void Drop()
    60.     {
    61.         if (item == null)
    62.             return;
    63.         else
    64.         {
    65.             item.GetComponent<AnimalAI>().FeathersOff();
    66.             item.GetComponent<Rigidbody>().useGravity = true;
    67.             item.GetComponent<Rigidbody>().isKinematic = false;
    68.             item.GetComponent<Collider>().enabled = true;
    69.             agent.enabled = true;
    70.             item.transform.parent = null;
    71.             item.transform.position = tempParent.transform.position;
    72.             item = null;
    73.         }
    74.     }
    75. }
     
  16. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    I like it so far. Visually is very nice I think. And it's great to see you creating a game based on (I guess) a story from the Bible. I have thought many times about making some Bible/Christian-based games. Never got around to it yet but anyway still think it is cool to see you making one. I put a Watch on this thread to follow along.

    Oh and yes your game will definitely share some things with others. And that's probably better to have things people understand and then add just enough innovation and personality to make the game stand out for that. A lot of that should just happen on its own unless a person is outright copying another game. And with you actively looking for ways to innovate you should be fine.

    Good luck!
     
    Last edited: Jan 24, 2019
  17. starmind001

    starmind001

    Joined:
    Jul 20, 2010
    Posts:
    95
    In this update I was able to fix a few issues with the controls and some of the scripts. I also added a random audio for the animals and set a distance in which the player can hear them. Just for fun I added a quest system, although it still needs some spit and polish, it seems to work well. I did see some issues during this last test, but shouldn't be a problem to fix.

    Next I would like to implement a good targeting system, a few more weapon upgrades, a mini boss, a big boss, a vendor system, a better controller system and a sneaking mechanic. All of these things will be used in some form or another.

    Day 9 Build:
     
    GarBenjamin likes this.
  18. starmind001

    starmind001

    Joined:
    Jul 20, 2010
    Posts:
    95
    I have a targeting system now, but it is not going the way it was planned. Sometime ago I made this targeting system, never really finished it. Did some playing around with it tonight, but I can't seem to get it to only target within range or within the screen view. Without a range it finds all enemies and will target anywhere on the map. So I disabled the current enemy AI and added some wood dummies for testing.
    Also tried to get the player to do a little z tracking when locked on, but I was only able to get the player to look at the target. This part is done in a few tutorials, but to me a rough code and not really more than a test to see if it could be done. One tutorial I tried would have required a complete rewrite of all my code I currently have working. So I am hoping to fix that. With some pointers in the right way to go.

    What I need the code to do:
    • To only target enemies within a certain distance of the player.(currently targets all enemies)
    • The player to rotate around enemies when locked on.(currently player only looks at target)
    • Remove target as a potential after being dealt with.(currently states that gameobject is missing)
    • Lastly, remove enemy targets if the player leaves range.(currently holds all enemies)
    Below is the target lock script. I know that Burgzerg arcade is the biggest contributor to this code the rest is me.

    TargetLock Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class TargetLock : MonoBehaviour
    7. {
    8.     public List<Transform> targets;
    9.     public Transform selectedTarget;
    10.     public GameObject player;
    11.     public Image image;
    12.     Camera cams;
    13.     public float Speed = 1.0f;
    14.     public float radius;
    15.    
    16.    
    17.     private Transform myTrans;
    18.    
    19.     void Start ()
    20.     {
    21.         targets = new List<Transform>();
    22.         selectedTarget = null;
    23.         myTrans = transform;
    24.         cams = Camera.main;
    25.        
    26.         Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
    27.         foreach (Collider nearbyObject in colliders)
    28.         {
    29.             if (nearbyObject.gameObject.tag == "Enemy")
    30.             {
    31.                 AddEnemies();
    32.             }
    33.  
    34.         }
    35.     }
    36.  
    37.     public void AddEnemies()
    38.     {
    39.         GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
    40.        
    41.         foreach(GameObject enemy in go)
    42.             AddTarget(enemy.transform);
    43.     }
    44.    
    45.     public void AddTarget(Transform enemy)
    46.     {
    47.         targets.Add(enemy);
    48.     }
    49.    
    50.     private void SortByDistance()
    51.     {
    52.         targets.Sort(delegate (Transform t1, Transform t2)
    53.         {
    54.             return (Vector3.Distance(t1.position, myTrans.position).CompareTo(Vector3.Distance(t2.position, myTrans.position)));
    55.         });
    56.     }
    57.    
    58.     private void TargetEnemy()
    59.     {
    60.         if(selectedTarget == null)
    61.         {
    62.             SortByDistance();
    63.             selectedTarget = targets[0];
    64.         }
    65.         else
    66.         {
    67.             int index = targets.IndexOf(selectedTarget);
    68.            
    69.             if(index < targets.Count - 1)
    70.             {
    71.                 index++;
    72.             }
    73.             else
    74.             {
    75.                 index = 0;
    76.             }
    77.             Deselected();
    78.             selectedTarget = targets[index];
    79.         }
    80.         Selected();
    81.     }
    82.    
    83.     private void Selected()
    84.     {
    85.         selectedTarget.GetComponentInChildren<Renderer>().material.color = Color.red;
    86.         myTrans.LookAt(selectedTarget);
    87.         //image.transform.position = cams.WorldToScreenPoint(selectedTarget.position);
    88.     }
    89.    
    90.     private void Deselected()
    91.     {
    92.         selectedTarget.GetComponentInChildren<Renderer>().material.color = Color.white;
    93.     }
    94.    
    95.     void FixedUpdate()
    96.     {
    97.         if(Input.GetButtonDown("Fire3"))
    98.         {
    99.            
    100.             //image.enabled = true;
    101.             TargetEnemy();
    102.         }
    103.         if(Input.GetButtonUp("Fire3"))
    104.         {
    105.             //image.enabled = false;
    106.             Deselected();
    107.         }
    108.     }
    109.  
    110.     private void OnDrawGizmosSelected()
    111.     {
    112.         Gizmos.color = Color.green;
    113.         Gizmos.DrawWireSphere(transform.position, radius);
    114.     }
    115. }
    116.  
     
  19. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    In Start you are using OverlapSphere to find enemies within range, but when you call AddEnemies, it's doing this:
    GameObject.FindGameObjectsWithTag("Enemy");
    so you're actually adding all of the enemies (multiple times, even) and that has nothing to do with the enemies in your overlap sphere. Maybe in your Start function, you meant to call
    AddTarget(nearbyObject);
    instead?
     
  20. starmind001

    starmind001

    Joined:
    Jul 20, 2010
    Posts:
    95
    I have sometime to work on this tonight, so I will give it another go. Thanks!
     
  21. starmind001

    starmind001

    Joined:
    Jul 20, 2010
    Posts:
    95
    Last night I worked on the script, but I must have been a little to tired to make sense of the code I was writing. I had to rewrite it tonight. :D

    Tonight I was able to get the code to work the way I wanted with the exception that it still grabs all enemies in scene. I tried removing
    GameObject.FindGameObjectsWithTag("Enemy");
    and just use
    AddTarget(nearbyObject);


    Problem with that is the code uses a transform to work. Simple fix, but still not working the way I needed. I bypassed the code using the second array, but it still needs that array to work.

    So after alot of rewrites, I came up with the code below. It is no gem, but it works, mostly.

    The code now restricts the player from targeting any enemy outside of range. It also removes the array if the player is not close to an enemy. This is a good thing. Next is the bad thing.

    My current problem is the player will lookat an enemy that is not selected and it continues to add other enemies that are not within range. Meaning it still adds enemies that are in the entire scene and adds them. I realize that it is because of the double arrays in the script, but it is necessary.

    Is there a way to stop any array element from being added later in the script below? I have established that only to add when in radius and to remove arrays if not within radius, but I cannot seem to control what is added.

    Below is a video and code in which you can see where the problem is and that the code works even with these exceptions. Also a pic of the player looking at an enemy off camera and you can see that it is looking a selectedtarget that is not even selected, in fact it is null.

    Pic:


    Video:


    Code:
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class TargetLock : MonoBehaviour
    5. {
    6.     public List<Transform> targets;
    7.     public Transform selectedTarget;
    8.     public GameObject player;
    9.     Camera cams;
    10.     public float radius;
    11.    
    12.     private Transform myTrans;
    13.    
    14.     void Start ()
    15.     {
    16.         targets = new List<Transform>();
    17.         selectedTarget = null;
    18.         cams = Camera.main;
    19.     }
    20.  
    21.     private void Update()
    22.     {
    23.         Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
    24.         foreach (Collider nearbyObject in colliders)
    25.         {
    26.             if (nearbyObject.gameObject.tag == "Enemy" && targets.Count <= 1 && selectedTarget == null)
    27.             {
    28.                 AddEnemies();
    29.             }
    30.         }
    31.         if (Vector3.Distance(selectedTarget.position, transform.position ) > radius)
    32.         {
    33.             targets.Remove(selectedTarget);
    34.             targets.Clear();
    35.             selectedTarget.GetComponentInChildren<Renderer>().material.color = Color.white;
    36.             selectedTarget = null;
    37.             Deselected();
    38.         }
    39.     }
    40.  
    41.     public void AddEnemies()
    42.     {
    43.         GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
    44.  
    45.         foreach (GameObject enemy in go)
    46.             AddTarget(enemy.transform);
    47.     }
    48.  
    49.     public void AddTarget(Transform enemy)
    50.     {
    51.         targets.Add(enemy);
    52.     }
    53.  
    54.     private void SortByDistance()
    55.     {
    56.         targets.Sort(delegate (Transform t1, Transform t2)
    57.         {
    58.             return (Vector3.Distance(t1.position, this.transform.position).CompareTo(Vector3.Distance(t2.position, this.transform.position)));
    59.         });
    60.     }
    61.  
    62.     private void TargetEnemy()
    63.     {
    64.         if (selectedTarget == null)
    65.         {
    66.             SortByDistance();
    67.             selectedTarget = targets[0];
    68.         }
    69.         else
    70.         {
    71.             int index = targets.IndexOf(selectedTarget);
    72.  
    73.             if (index < targets.Count - 1)
    74.             {
    75.                 index++;
    76.             }
    77.             else
    78.             {
    79.                 index = 0;
    80.             }
    81.             Deselected();
    82.             selectedTarget = targets[index];
    83.         }
    84.         Selected();
    85.     }
    86.  
    87.     private void Selected()
    88.     {
    89.         selectedTarget.GetComponentInChildren<Renderer>().material.color = Color.red;
    90.         Vector3 targetPostition = new Vector3(selectedTarget.position.x,
    91.                                         this.transform.position.y,
    92.                                         selectedTarget.position.z);
    93.         this.transform.LookAt(targetPostition);
    94.     }
    95.  
    96.     private void Deselected()
    97.     {
    98.         selectedTarget.GetComponentInChildren<Renderer>().material.color = Color.white;
    99.     }
    100.  
    101.     void FixedUpdate()
    102.     {
    103.         if (Input.GetButtonDown("Fire3"))
    104.         {
    105.             TargetEnemy();
    106.         }
    107.         if (Input.GetButtonUp("Fire3"))
    108.         {
    109.             Deselected();
    110.         }
    111.     }
    112.  
    113.     private void OnDrawGizmosSelected()
    114.     {
    115.         Gizmos.color = Color.green;
    116.         Gizmos.DrawWireSphere(transform.position, radius);
    117.     }
    118. }
    119.  
     
  22. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Your code still has the exact same problem that I pointed out in my last comment. You only want to add enemies that are in your overlap sphere, but your AddEnemies function doesn't make sense. It is just adding all of the enemies with the enemy tag, whether they're in range or not. In fact, I don't see what the point of the AddEnemies function is, when you could just call
    AddTarget(nearbyObject.transform); 
    instead.
     
  23. starmind001

    starmind001

    Joined:
    Jul 20, 2010
    Posts:
    95
    You are correct! But it is only adding one enemy at a time. Meaning I can have 3 enemies, but I can only target one. I had to put in
    targets.Count <= 1 
    or it would just add all enemies forever. When I say forever, I mean crash unity forever.

    That is why I left the
    AddEnemies(); 
    function as it worked with the exceptions I was having. Actually I don't really need to call
    AddTarget(nearbyObject.transform);
    as I can just call
    targets.Add(nearbyObject.transform);
    instead.

    The code works both ways, add all enemies in scene or one at a time. The only other problem I am seeing is targets are not clearing on exit radius. I have to press the target button to clear it. If I don't, it remembers. (Clever girl);)

    Code:
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. public class TargetLock : MonoBehaviour
    4. {
    5.     public List<Transform> targets;
    6.     public Transform selectedTarget;
    7.     public GameObject player;
    8.     Camera cams;
    9.     public float radius;
    10.  
    11.     private Transform myTrans;
    12.  
    13.     void Start ()
    14.     {
    15.         targets = new List<Transform>();
    16.         selectedTarget = null;
    17.         cams = Camera.main;
    18.     }
    19.     private void Update()
    20.     {
    21.         Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
    22.         foreach (Collider nearbyObject in colliders)
    23.         {
    24.             if (nearbyObject.gameObject.tag == "Enemy" && targets.Count <= 1 && selectedTarget == null)
    25.             {
    26.                 //AddEnemies();
    27.                 targets.Add(nearbyObject.transform);
    28.             }
    29.         }
    30.         if (Vector3.Distance(selectedTarget.position, transform.position ) > radius)
    31.         {
    32.             targets.Remove(selectedTarget);
    33.             targets.Clear();
    34.             selectedTarget.GetComponentInChildren<Renderer>().material.color = Color.white;
    35.             selectedTarget = null;
    36.             Deselected();
    37.         }
    38.     }
    39.   //  public void AddEnemies()
    40.   //  {
    41.     //    GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
    42.     //    foreach (GameObject enemy in go)
    43.      //       AddTarget(enemy.transform);
    44.  //   }
    45.   //  public void AddTarget(Transform enemy)
    46.   //  {
    47.   //      targets.Add(enemy);
    48.   //  }
    49.     private void SortByDistance()
    50.     {
    51.         targets.Sort(delegate (Transform t1, Transform t2)
    52.         {
    53.             return (Vector3.Distance(t1.position, this.transform.position).CompareTo(Vector3.Distance(t2.position, this.transform.position)));
    54.         });
    55.     }
    56.     private void TargetEnemy()
    57.     {
    58.         if (selectedTarget == null)
    59.         {
    60.             SortByDistance();
    61.             selectedTarget = targets[0];
    62.         }
    63.         else
    64.         {
    65.             int index = targets.IndexOf(selectedTarget);
    66.             if (index < targets.Count - 1)
    67.             {
    68.                 index++;
    69.             }
    70.             else
    71.             {
    72.                 index = 0;
    73.             }
    74.             Deselected();
    75.             selectedTarget = targets[index];
    76.         }
    77.         Selected();
    78.     }
    79.     private void Selected()
    80.     {
    81.         selectedTarget.GetComponentInChildren<Renderer>().material.color = Color.red;
    82.         Vector3 targetPostition = new Vector3(selectedTarget.position.x,
    83.                                         this.transform.position.y,
    84.                                         selectedTarget.position.z);
    85.         this.transform.LookAt(targetPostition);
    86.     }
    87.     private void Deselected()
    88.     {
    89.         selectedTarget.GetComponentInChildren<Renderer>().material.color = Color.white;
    90.     }
    91.     void FixedUpdate()
    92.     {
    93.         if (Input.GetButtonDown("Fire3"))
    94.         {
    95.             TargetEnemy();
    96.         }
    97.         if (Input.GetButtonUp("Fire3"))
    98.         {
    99.             Deselected();
    100.         }
    101.     }
    102.     private void OnDrawGizmosSelected()
    103.     {
    104.         Gizmos.color = Color.green;
    105.         Gizmos.DrawWireSphere(transform.position, radius);
    106.     }
    107. }
     
  24. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    In your code, You only calling targets.clear(); when the selectedtarget leaves the radius, but nothing happens whenever other enemies leave the radius. Could that be the problem? I'm sort-of guessing here, because I'm having trouble figuring-out how it's all supposed to work at this point.
     
  25. starmind001

    starmind001

    Joined:
    Jul 20, 2010
    Posts:
    95
    Me too! One question did you test it before giving your input or just looked at the script and said "oh there is the problem"? I think it might have made more sense if you had tried my script. I know how rude of me, but not my intention.
    I have fixed the issue with it not clearing when exiting, but now I have royally messed up the script, so it looks like this old script is not going to work for me for now. I personally didn't want a target lock system, but it was brought to my attention that my game needed one. Maybe someday I will come back to this and make a script that works. Not giving up on it, just the script, at the moment, looks like a jumbled mess and my ocd is going nuts.

    Hmm.... homing projectiles
     
    Last edited: Jan 30, 2019
  26. starmind001

    starmind001

    Joined:
    Jul 20, 2010
    Posts:
    95
    I couldn't let it go. Tonight, I was able to find a code that works for me. So far it has only one problem that I cannot fix.

    The problem I am having is when the target is destroyed, it is not going to the next enemy or if there is no more enemies, it is not removing the target.

    This problem is not a bad problem. Meaning it doesn't effect gameplay that bad. Just have to hit the button one more time to clear instead of automatic.

    Thanks for the help!

    Code:
    Code (CSharp):
    1.  
    2. {
    3.     Camera cam;
    4.     public enemyInView target;
    5.     Image image;
    6.     GameObject player;
    7.  
    8.     bool lockedOn;
    9.     public int lockedEnemy;
    10.  
    11.     public static List<enemyInView> nearByEnemies = new List<enemyInView>();
    12.  
    13.     void Start()
    14.     {
    15.         cam = Camera.main;
    16.         image = gameObject.GetComponent<Image>();
    17.         player = GameObject.FindGameObjectWithTag("Player");
    18.  
    19.         lockedOn = false;
    20.         lockedEnemy = 0;
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.         if (Input.GetButtonDown("Fire3") && !lockedOn)
    26.         {
    27.             if (nearByEnemies.Count >= 1)
    28.             {
    29.                 lockedOn = true;
    30.                 image.enabled = true;
    31.                 lockedEnemy = 0;
    32.                 target = nearByEnemies[lockedEnemy];
    33.                 player.transform.LookAt(target.transform.position);
    34.             }
    35.  
    36.         }
    37.         else if ((Input.GetButtonDown("Fire3") && lockedOn) || nearByEnemies.Count == 0)
    38.         {
    39.             lockedOn = false;
    40.             image.enabled = false;
    41.             lockedEnemy = 0;
    42.             target = null;
    43.             nearByEnemies.Remove(target);
    44.         }
    45.  
    46.         if (Input.GetKeyDown(KeyCode.X))
    47.         {
    48.             if (lockedEnemy == nearByEnemies.Count - 1)
    49.             {
    50.                 lockedEnemy = 0;
    51.                 target = nearByEnemies[lockedEnemy];
    52.             }
    53.             else
    54.             {
    55.                 lockedEnemy++;
    56.                 target = nearByEnemies[lockedEnemy];
    57.                 player.transform.LookAt(target.transform.position);
    58.             }
    59.         }
    60.  
    61.         if (lockedOn)
    62.         {
    63.             target = nearByEnemies[lockedEnemy];
    64.             gameObject.transform.position = cam.WorldToScreenPoint(target.transform.position);
    65.             gameObject.transform.Rotate(new Vector3(0, 0, -1));
    66.  
    67.             if (!target.isActiveAndEnabled)
    68.             {
    69.                 lockedOn = false;
    70.                 image.enabled = false;
    71.                 lockedEnemy = 0;
    72.                 target = null;
    73.             }
    74.         }
    75.     }
    76. }
    77.  
    78.  
     
  27. starmind001

    starmind001

    Joined:
    Jul 20, 2010
    Posts:
    95
    Tonight I have had sometime to work more on my target script. I am getting an out of range error when all enemies are destroyed. I cannot seem to remove the target and disable this script when all enemies are gone. If I let the error run and find another enemy it will reset until the enemy is destroyed.

    Is there a way to fix this?

    Code (CSharp):
    1. ublic class targetController : MonoBehaviour
    2. {
    3.     Camera cam;
    4.     public enemyInView target;
    5.     Image image;
    6.     GameObject player;
    7.  
    8.     bool lockedOn;
    9.     public int lockedEnemy;
    10.  
    11.     public static List<enemyInView> nearByEnemies = new List<enemyInView>();
    12.  
    13.     void Start()
    14.     {
    15.         cam = Camera.main;
    16.         image = gameObject.GetComponent<Image>();
    17.         player = GameObject.FindGameObjectWithTag("Player");
    18.  
    19.         lockedOn = false;
    20.         lockedEnemy = 0;
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.         target = nearByEnemies[lockedEnemy];
    26.         if (target == null)
    27.         {
    28.             nearByEnemies.RemoveAll(target => target == null);
    29.  
    30.         }
    31.  
    32.         if (Input.GetButtonDown("Fire3") && !lockedOn)
    33.         {
    34.             LockOnToTarget();
    35.         }
    36.         else if ((Input.GetButtonDown("Fire3") && lockedOn) || nearByEnemies.Count == 0)
    37.         {
    38.             lockedOn = false;
    39.             image.enabled = false;
    40.             lockedEnemy = 0;
    41.             target.GetComponentInChildren<Renderer>().material.color = Color.white;
    42.             target = null;
    43.             nearByEnemies.Remove(target);
    44.         }
    45.  
    46.         if (Input.GetKeyDown(KeyCode.X))
    47.         {
    48.             if (lockedEnemy == nearByEnemies.Count - 1)
    49.             {
    50.                 target.GetComponentInChildren<Renderer>().material.color = Color.white;
    51.                 lockedEnemy = 0;
    52.                 target = nearByEnemies[lockedEnemy];
    53.             }
    54.             else
    55.             {
    56.                 nearByEnemies[lockedEnemy].gameObject.GetComponentInChildren<Renderer>().material.color = Color.white;
    57.                 lockedEnemy++;
    58.                 target = nearByEnemies[lockedEnemy];
    59.                 //player.transform.LookAt(target.transform.position.normalized);
    60.                 Vector3 targetPostition = new Vector3(target.transform.position.x,
    61.                  player.transform.position.y,
    62.                    target.transform.position.z);
    63.                 player.transform.LookAt(targetPostition);
    64.             }
    65.         }
    66.  
    67.         if (lockedOn)
    68.         {
    69.             if (target.isActiveAndEnabled)
    70.             {
    71.                 target = nearByEnemies[lockedEnemy];
    72.                 gameObject.transform.position = cam.WorldToScreenPoint(target.transform.position);
    73.                 gameObject.transform.Rotate(new Vector3(0, 0, -1));
    74.                 target.GetComponentInChildren<Renderer>().material.color = Color.red;
    75.                 // player.transform.LookAt(target.transform.position.normalized);
    76.                 Vector3 targetPostition = new Vector3(target.transform.position.x,
    77.                         player.transform.position.y,
    78.                           target.transform.position.z);
    79.                 //player.transform.LookAt(targetPostition);
    80.             }
    81.             else if (!target.isActiveAndEnabled)
    82.             {
    83.                 if (lockedEnemy == nearByEnemies.Count - 1)
    84.                 {
    85.                     lockedEnemy--;
    86.                 }
    87.                 else
    88.                 {
    89.                     lockedOn = false;
    90.                     image.enabled = false;
    91.                     lockedEnemy = 0;
    92.                     target.GetComponentInChildren<Renderer>().material.color = Color.white;
    93.                     walkAway();
    94.                 }
    95.             }
    96.         }
    97.     }
    98.  
    99.     void LockOnToTarget()
    100.     {
    101.         if (nearByEnemies.Count >= 1)
    102.         {
    103.             lockedOn = true;
    104.             image.enabled = true;
    105.             lockedEnemy++;
    106.             target = nearByEnemies[lockedEnemy];
    107.             //player.transform.LookAt(target.transform.position.normalized);
    108.             Vector3 targetPostition = new Vector3(target.transform.position.x,
    109.                 player.transform.position.y,
    110.                 target.transform.position.z);
    111.             player.transform.LookAt(targetPostition);
    112.             target.GetComponentInChildren<Renderer>().material.color = Color.white;
    113.         }
    114.     }
    115.     public void walkAway()
    116.     {
    117.         lockedOn = false;
    118.         image.enabled = false;
    119.         lockedEnemy = 0;
    120.         target.GetComponentInChildren<Renderer>().material.color = Color.white;
    121.         target = null;
    122.         nearByEnemies.Remove(target);
    123.     }
    124. }
     
  28. Starmind01

    Starmind01

    Joined:
    May 23, 2019
    Posts:
    78
    Small update on this. I have been working on a few aspects not really shown in the video, but I am still working on this.

    Part # 11



    In the video you can see small changes and some glitches. Believe it or not, the glitches went away after saving the scene and restarting unity. I guess unity can only take so many changes before it starts to crash.

    What I have changed:
    • I replaced the NPC Cube with an actual NPC model
    • Gave the chickens some color variations
    • Added a functional targeting system
    • Gave the guards the ability to duck/evade an attack from the player
    • Added a button mash for the blood on the doors(I have a plan for this that comes later)
    • Added code so player cannot pickup chickens after being put in corral
    • Added an active quest reminder at the bottom of the scene
    • Added a trigger event to change lighting(I have a plan for this later)

    Some glitches I have noticed or things not working As I want them to:

    • When the chickens are placed in the corral the map icon should disappear, but don't. Only able to turn all off at the end.
    • Chicken map icons turn on at random when the player attacks
    • Button mash code not playing animation sometimes
    • Loot script providing to much loot(percentage to high?)
    • When player attacks while walking or running, the player stops for a sec and then resumes(no crossfade?)

    Needs and wants:

    • An editor tool extension to make a NPC with all the scripts and physics(save time later)
    • A better dialogue system(Brakeys youtube tutorial not working the way I had hoped)
    • More 3d models that fit the same feeling of the game
    • Eye and mouth animations(Animated texture(zelda) or Sprite animation)
    • Cutscenes
    • More level design
    • Better xbox controller input(Invector style)
    • Menus(main menu, pause, inventory and options)
    • Original level/ background music(skyrim like, but not repetitive)

    Well that pretty much does it for now. If you want to try it out and give some pointers feel free to try it here, at itch.io.
    Please note you will need a xbox controller to play for some of the controls, like targeting and the button mash. Since I did the upload in a hurry, I did notice that I need the mouse on the fist dialogue box.

    The Xbox controls are:
    • Left stick - movement
    • A - fire projectile/ accept quest
    • B - bomb
    • LB - target enemy
    • X - action/ button mash/ pickup
    The Keyboard controls are:
    • WASD/ arrows keys - movement
    • Left mouse - fire projectile/ accept quest
    • Right mouse - bomb
    • Middle mouse - pickup
    • No target button or button mash assigned
     
  29. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Hi, 1st time in here.

    That game is looking good!

    Must admit I'm not a huge fan of christian games BUT Moses has a "super hero" kinda vibe to him so depending on how you wrap things up this game could have an appeal outside christian circles. Just gotta keep a good balance (really depends on what you're aiming for).

    I agree with Serix concerning the inconsistent look. I suggest looking into color grading. It does wonders to bring everything together. BTW do you still have problems with targets out of range? What you have to do is check if the target is valid before trying to use it, if it isn't, it's time to get a new target or leave it null when none are found.

    Regarding your glitches, I suggest checking out a bunch of debugging tutorial. Those issues are all pretty trivial once you know your way around unity. What I can tell you is that its ALMOST always your own fault when code doesn't work as intended. Bugs happen but it's much more rare. What you need to do is triple check your code line by line and add some debug output to confirm values are what you think they are. What really helped me when starting was to comment every single lines in my code.

    About your needs:
    NPC editor tool with physics
    I don't know any (haven't looked) but I know there are complete projects you can purchase on the store but then you end up just making a theme for a game that already exists. Still, it helps understand things work.

    Dialog system: Sorry haven't tried any and I will probably whip up my own when needed but this one seems recommended https://assetstore.unity.com/packages/tools/ai/dialogue-system-for-unity-11672

    3D models: The app store is full of that. Maybe try the new visual search tool from unity? Check out: https://assetstore.unity.com/packages/tools/utilities/unity-visual-search-104059

    Eye and mouth animations: You may want to look into making your own. I remember seeing a sprite sheet with a ton of eye and mouths that could be used to do that but I dont remember were. Maybe you can find something here https://opengameart.org

    Cut scenes: Unity's solution is excellent, take a look at Cinemachine https://assetstore.unity.com/packages/essentials/cinemachine-79898 I know it's a complex plugin but its worth every second you will put into learning it. Besides, if you are not ready to RTFM and learn new things what are you doing trying to make a game? :p

    More level design: This is what we use: https://assetstore.unity.com/packages/tools/level-design/octave3d-level-design-45021 makes everything much simpler and faster

    Better Xbox controller input: Not sure what you're asking for here; if you want a drop in character controller solution I suggest checking out what Opsive and Otti offers on the asset store. In our case those required a lot of modifications for our use (complex parkour stuff), so we ended up making our own. But if you meant a better controller input system I suggest looking into Rewired. It fixed many issues for us and has an in game configuration menu as a prefab (hard to customize though). Make sure it's what you need though cause its one of those assets you shouldn't use without completely familiarizing yourself with the manual.

    Menus: There's a lot of solutions on the store, even free ones that will do an excellent job. Since you are starting, I suggest keeping your menus to a minimum and rely on unity's start screen to handle input remapping (unless you go with rewired).

    Original levels: Get octave 3D and let your imagination run free! Watch Ben-Hur for inspiration :p Not kidding they've got awesome visuals in there, even by today's standards. No matter your take on religion, it's a great movie, can you believe It was filmed in 1959!?!

    Background music: https://opengameart.org has some, there are also a TON of music packs in the store and this other website I found with a lot of great stuff https://gamesounds.xyz/ You can also use outsourcing services like https://www.fiverr.com/ if you need custom music/art/code/whatever on the cheap (can also be expansive if you want to).

    Regarding your controls, I noticed your character's movement isn't smooth. That's not caused by your input. Your animations are the problem or rather the way you built your state machine in the animator controller.

    When you stop walking you just stop playing the animation so it stops moving instantly. To avoid this you have to either whip up some code to smoothly slow down the animation speed (you could try multiplying the animation speed with the input value for a crude fix but without also multiplying it with a curve you may not like the results) OR insert a "start to walk" and "stop to walk" animation before and after you walk animation.

    Well that's enough for tonight, hope all that helps.

    Let us know what you're up to.
    Good luck!
     
    Last edited: Oct 16, 2019