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. Dismiss Notice

Need monster script

Discussion in 'Scripting' started by siren, Apr 14, 2014.

  1. siren

    siren

    Joined:
    Jun 13, 2013
    Posts:
    2
    okay i want to do this :
    Monster appear on random places every two min
    And hunt down a player if he was turning the flash on
    And when the monster is close to the player
    This screen turn red and it's shaking
    If you turn the flash off the monster stop chasing him and after seven seconds the monster disappear
    If he was very close he will not stop even if the flesh is off
    He will chase the player for 20 to 30 seconds
    The player died from three hits.

    so could anyone help me with this scripts please .
     
  2. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    Learn scripting. You're basically asking community to make an AI for you!
     
  3. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    This is more than an AI... this is a game prototype.

    1. You will need a script that spawns the monster on a timer. To start with, learn about Instantiate and Coroutines.

    2. You will need a script on the player that toggles on and off the 'flash' (flashlight?). Input.GetKeyDown can detect a keypress that you can use to toggle a bool for this.

    3. You will need a script attached to the monster that locates the player gameObject. You could keep a reference to the player on your monsterSpawner script from step 1, and pass the player to the monster.

    This script should then make the monster move towards the player if the player's flash is on, or if it has been less than 7 seconds since it was turned off. You can keep a reference to the player's flash component to easily check against this - you will likely need to use GetComponent for this when the monster is spawned.

    4. The monster movement script should also track the distance between the player and the monster, so that the monster can continue to chase the player if he is close enough and the flash is off. Vector3.Distance is an easy way to check.

    The distance can also be used to see if the screen should be turning red or not. There are a few ways to do this - but you will need another gameobject in the scene that has a script that controls this. The OnGUI function call can display things on the screen - I'm sure a Gui.Rectangle the size of the screen with a mostly transparent red color will work. Increase the opacity as the monster gets closer.

    Shaking the screen would involve some random motion of your camera. Camera.main will access your camera, which you can apply random translations or rotations to if the monster is close to the player. Just make sure they are small, and that you keep track of the base orientation,

    5. You will need some way of tracking how the monster touches the player. OnTriggerEnter or OnCollisionEnter can determine if the two entities collide - you could use that, but you would need the two entities to separate again to detect another collision. You could just say that if the entities are close enough (step 4), detect a hit.

    You may want some sort of timer to make sure hits don't happen to quickly (limit it to, say, once per second). Time.time can be useful for that, or look back into Coroutines.

    6. To despawn after 20-30 seconds, you can use Random.Range to randomly choose a number between 20 and 30, and call Destroy(GameObject, float) at the start script.

    7. Don't forget, the player needs some sort of basic health script too that the monster will need to notify when it hits him. When the player's life reaches zero, it will die.


    As you can see, for someone just starting out, there are a lot of different concepts that need to be looked at. Even the things I posted are not necessarily the best ways of approaching every topic. What you are asking for won't be accomplished by a beginner in any quick fashion - I advise you to look into tutorials and at the concepts I have bolded if you are interested in learning to script and develop your game idea. As you come across specific questions on concepts you dont understand, feel free to post your code attempts here and members are more than willing to help you out - provided it is clear you are attempting something on your own first.

    If you want someone to make a script for you, this is not the place to ask. People don't usually do individual scripts, much less entire prototypes, for free. The Commercial: Job Seeking forum has coders offering to do jobs, or you can post a topic in the Commercial: Job Seeking forum - This is paid work only.

    Best of luck with your game.
     
  4. siren

    siren

    Joined:
    Jun 13, 2013
    Posts:
    2
    Thanks Zaladur that's really helpful