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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

one really important scripts for our first game

Discussion in 'Scripting' started by Simigla, Aug 24, 2015.

  1. Simigla

    Simigla

    Joined:
    Aug 20, 2015
    Posts:
    4
    hello guys, we are a small team who really want to create their first game and working really hard on it. but we are quite beginners, and we tried to make this script ,but we never found a way for it :( please can anybody tell me this script?

    1. stealth system. (in this i mean we have the characters and everything fit in the scene we just want for example:
    I AM the PLAYER, in a house where the owner of the house can't see me, and if he saw me, he attack me nad kill me.
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You shouldn't ask for complete scripts here. I'd recommend to watch tutorials first if you don't know how to approach this.
     
    landon912 likes this.
  3. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Noone is going to write the script for you here, like Suddoha said, but we can help you on the way.

    For example, I can see at least 6 different scripts in your example:
    2) A player input script (for catching user input)
    3) An enemy AI script (for simulating input)
    1) A movement script (for handling movement for the player and the enemy
    2) A vision script (checks to see if the enemy can see the player (based on line of sight (raycast)?))
    3) A script that handles attacking
    4) A script that handles dying

    Look up tutorials for each of these types of script, and you should be able to get it all working :)
     
    landon912 likes this.
  4. Recon03

    Recon03

    Joined:
    Aug 5, 2013
    Posts:
    839
    Think about it for a minute . You can set up a distance from house owner to player, and use a radius around the house owner so he can only see so far around him.


    Something like this can get you started.. I made a game like this last year. I won't give you code, since you will never learn. But try to look at this video, surprised you did not find this on your own, I type stealth Unity, etc..

     
  5. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Well this is a huge topic to cover. First you need to be able to do the basic gameplay scripting. You need to have a knowledge on classes like Vector3, Transform, Quaternion, Navmesh and such, so that you can write the codes necessary for that. As i said, no one will have time to give you whole scripts and explain the process, even if they do, you would have lots of trouble with implementing it since you guys are beginners. And if you find any ready scripts from the web, you would still have problem implementing them.

    But there is still hope, I can tell you the logic of the system you are trying to achieve. Then once you guys learn the basic gameplay programming, you can start building your system.

    So, we have a character. Player must be able to walk around the meshes, with colliders in order to prevent it from colliding with the 3D world. Unity has a basic class for that, which is CharacterController. You can use CharacterController for your class, and write the basics of your movement using that class. It is pretty simple, you can find lots of tutorials about it. So lets say we got the player and the camera movement covered.

    Now, we have the owner of the house. An AI. While programming the AI logic, best approach would be using a simple logic for now. So, owner of the house would patrol over the house randomly right? In order to achieve that, you need to have an Array of waypoints around the house. You can make the AI randomly select one of the waypoints and move to them, wait over the waypoints for some random time, and then move to another waypoint, simulating a common patrol behaviour. You can find a lot of tutorials about creating random waypoints and making your AI select them over the internet.

    Another thing, is the AI's movement. You can achieve movement by using CharacterController, Transform.Translate, Vector3.Lerp, and many other things. But I advice you to use Navmesh class for that. Navmeshes are the areas which an AI can walk to, you can set the areas by Window > Navigation tab. You need to add the necessary Navmesh components to your AI, and write the movement & turning logic using that, since "the house" is a place where there are lots of narrow corners and lots of objects. You wouldn't want your AI to pass through things or get stuck by objects, thus the best solution is using Navmesh. And again, you can find a lot of tutorials over the internet.

    Now first thing to do when writing detection behaviour is to check the distance between your AI and player. If the distance is close enough, the AI will be able to do the other necessary calculations ( like angle calculations in order to understand if the player is in sight) You can check the distance between two points ( which is player's position and the AI's position in this case ) by simply using the helper function called Vector3.Distance. You pass in two Vector3 variables, and this functions will return you a float variable in order to check if the distance is close or long enough.

    One more thing I want to talk about is the angle. How can the AI detect the player? It's actually pretty simple, you need to do some Raycasting and Vector math. We have two ways to achieve this :

    • Simple one, create a raycast which starts from the AI's position, and goes forward. If this ray touches the player, it means there are no obstacles around, and player is standing right in front of the AI, so the you can understand that the AI detects player now. But this is a very basic way, and will only work if the player is standing right in front of the AI.

    • A better way to approach is using Vector3.Angle. This helper function calculates the angle between two vectors and returns a float value to you. So, just think about it. We have a vector which is achieved by substracting the AI's position from player's position. ( E.g player.transform.position - enemy.transform.position, note that substraction must be calculated in the Update function so we can have a stable Vector in each frame, but ofcourse, you can make the necessary boolean checks to prevent AI from trying to calculate the Vector each frame, for example a distance check. If the player is too far away from the AI, there is no need to check for the angle ) So think about it, this is a vector which starts on the AI's position, and ends on the player's position. Lets call this the VectorA.

    Now we need a second vector, which will simply be the AI's forward vector. A straight vector starting from the AI's position and pointing straight forward. Call this VectorB.

    So create 2 float variables, one will be adjusted through inspector and call it f_NeededAngle. Other one will be a private variable and call it f_AngleBetween. So simply doing this :

    Code (CSharp):
    1. f_AngleBetween = Vector3.Angle(VectorA, VectorB);
    will set the f_AngleBetween variable as the angle between the AI's forward and AI to player vectors. Thus, if this f_AngleBetween variable is 90 degrees, it means that player is standing on the right of the AI or the left of it. If this variable is 180, it means player is behind the AI. So, doing this:

    Code (CSharp):
    1. if(f_AngleBetween < f_AngleNeeded)
    will able you to do necessary actions if the player is in "sight" of the AI. By the way, if the player is in sight, you can also check if there are no obstacles between player and AI. Even if the player is in front of the AI according to the "Vector" calculations, there might be an object like a wall between them, thus AI shouldn't see the player. So, if the angle is enough, make a secondary check with raycasting. Cast a ray from the AI, to the player. ( Start of the ray will be AI's position and the direction will be AI's position substracted from player's position. Remember the VectorA ? Thats it. ) If this ray touches to player it means there are no objects between so the AI can perform the actions like attacking. If it touches something else, than it means there is an object between. ( Caution, you need to cast multiple rays with different angles and make them work cooporated in order to prevent very small objects to get between and trick the AI like it's not seeing the player where it actually is. But this is an advanced topic, you don't need this extra for now. ) By the way, you can check if a ray "touches" to any collider, by using Tag or CompareTag().

    As an extra, you can write "hearing" behaviour for your AI. If player emits a sound effect, you can check the distance between your player and your AI, and maybe reach to a script which you have written for sounds effect to have the distance variable of the sound that is casted. So you can calculate if the AI will be able to hear it or not. If it hears, AI can move to the position which the sound was casted and maybe see the player over there or move around randomly to simulate "inspection".

    Now once the detection is done, here comes the attacking part. In order to make the AI attack to your player, first you need to move your AI to the player. Again, you can use the Navmesh's move functions, then check the distance between, if the AI is close enough, it'll attack. You may need an attack animation, then you can create raycasts to check if those attacks touches to the player. If yes, you can call a public function -which you should have written on the player's health script- to damage the player. That health script can check the health of the player and perform the necessary actions like calling dying animation, or restarting the level, or any other thing.

    Well, simply the logic is this, but keep in mind that you have to practise a lot to be able to write these gameplay functions & logic as stable. Just start small, make an AI walk around, use some vectors, learn some other functions and eventually you will develop yourself and be able to implement the logics easily.

    Cheers,
    Inan
     
    Last edited: Aug 24, 2015
  6. Simigla

    Simigla

    Joined:
    Aug 20, 2015
    Posts:
    4
    okay guys thanks a lot :) i think we should be able to do it now! cheers :)