x


Slender like game

Hi, I came here because i have a very basic knowledge of C++. I am making a game like the horror game Slender, I have a character and i want him to randomly teleport around the map until he comes into the vicinity of the player. I also would like to be able to play a sound when he is in the general area of the player. I know that this is fairly complicated and i am sorry for that but i dont know where else to go to find out such specific scripts.

more ▼

asked Jul 20, 2012 at 10:18 AM

TheCradam gravatar image

TheCradam
-4 4 4 4

You really need to read the tutorials and I suggest getting the complete projects from the Unity Asset Store.

If you have only very basic knowledge of C++ then you need to pick a language, perhaps c# to work in and become proficient with that.

I'm afraid there's no substitue for experience. My suggestion would be that you do something a bit simpler as a knowledge gaining exercise. Maybe something as simple as a two-player version of noughts and crosses.

Jul 20, 2012 at 11:45 PM Bovine

http://slendergame.com/legend.php are you working on this title? @_@

Aug 02, 2012 at 02:51 PM Jeffom

Please amend your title - it has nothing to do with your question.

Aug 02, 2012 at 03:03 PM Bovine

Since this question seems to rise time and time again. I am going to link this to another question similar to this that has a fantastic answer. Anyone wanting to make a 'Slender' game please reference the approved answer on this question

Feb 18, 2013 at 06:38 AM Starkid225

suggest you learn about transform.position !!

Jul 20, 2012 at 10:53 AM Fattie
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

They are right, you will want to run through some tutorials to get familiar with Unity.

If you like C++ and are comfortable with it I would suggest using C# as your programming language.

I can provide you with a very basic script that you can and will need to add to in order to optimize it for your specific purpose.

C#

 using UnityEngine;
 using System.Collections;
 
 public class Teleporter : MonoBehaviour
 {
     public Transform player;      // the Object the player is controlling
     public Vector3 spawnOrgin;     // this will be the bottom right corner of a square we will use as the spawn area
     public Vector3 maximum;        // max distance in the x, y, and z direction the enemy can spawn
     public float spawnRate;        // how often the enemy will respawn
     public float distanceToPlayer; // how close the enemy has to be to the player to play music
 
     private bool nearPlayer = false; // use this to stop the teleporting if near the player
     private float nextTeleport = 0.0f; // will keep track of when we to teleport next

     void Start ()
     {
          nextTeleport = spawnRate;
     }

     void Update ()
     {
          if (!nearPlayer)     // only teleport if we are not close to the player
          {
             if (Time.time > nextTeleport)   // only teleport if enough time has passed
             {
               transform.position = new Vector3( Random.Range(spawnOrgin.x, maximum.x), Random.Range(spawnOrgin.y, maximum.y), Random.Range(spawnOrgin.z, maximum.z) );   // teleport
               nextTeleport += spawnRate;    // update the next time to teleport
             }
          }
          if (Vector3.Distance(transform.position, player.position) <= distanceToPlayer)
          {
              if (audio && audio.clip && !audio.isPlaying)     // play the audio if it isn't playing
                   audio.Play();
              nearPlayer = true;
          }
          else
          {
              if (audio)
                  audio.Stop();
              nearPlayer = false;
          }
     }
 
 }


Javascript

 var player : Transform;      // the Object the player is controlling
 var spawnOrgin : Vector3;     // this will be the bottom right corner of a square we will use as the spawn area
 var maximum : Vector3;        // max distance in the x, y, and z direction the enemy can spawn
 var spawnRate : float;        // how often the enemy will respawn
 var distanceToPlayer : float; // how close the enemy has to be to the player to play music
 
 private var nearPlayer : boolean = false; // use this to stop the teleporting if near the player
 private var nextTeleport : float = 0.0f; // will keep track of when we to teleport next

 function Start ()
 {
     nextTeleport = spawnRate;
 }

 function Update ()
 {
     if (!nearPlayer)     // only teleport if we are not close to the player
     {
         if (Time.time > nextTeleport)   // only teleport if enough time has passed
         {
             transform.position = Vector3( Random.Range(spawnOrgin.x, maximum.x), Random.Range(spawnOrgin.y, maximum.y), Random.Range(spawnOrgin.z, maximum.z) );   // teleport
             nextTeleport += spawnRate;    // update the next time to teleport
         }
     }
     if (Vector3.Distance(transform.position, player.position) <= distanceToPlayer)
     {
          if (audio && audio.clip && !audio.isPlaying)     // play the audio if it isn't playing
               audio.Play();
          nearPlayer = true;
     }
     else
     {
          if (audio)
             audio.Stop();
          nearPlayer = false;
     }
 }



The Inspector is your best friend. Attach this script to the enemy GameObject in your scene and edit the exposed variables (the public ones) in the Inspector window. To assign the player variable simply drag the player gameObject from the Hierarchy window into the variable field.

You will also need to attach and AudioSource component to the enemy GameObject and assign a clip to it in the Inspector. Take a look at the unity documentation and tutorials for further details on that.

more ▼

answered Jul 21, 2012 at 12:15 AM

Gamemaster gravatar image

Gamemaster
70 11 9 14

i get this error when trying to enable this script, Assets/Scripts/Slenderman.cs(13,34): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `Teleporter.spawnRate', and it is due to this part of the script, private float nextTeleport = spawnRate;

Does anybody know whats wrong with it??

Aug 02, 2012 at 02:09 PM jazza

You cannot set the field nextTeleport to the value of spawnRate unless spawn rate were const say.

You would need to set nextTeleport in Awake()

Aug 02, 2012 at 02:31 PM Bovine

Bovine is right, I modified the code to be correct. I wrote the code in a hurry. I also changed were I had max.x, max.y, and max.z to maximum.x, maximum.y, and maximum.z. I also Changed the if (Vector3.Distance(transform.position, player.position) >= distanceToPlayer) To <= instead. And some audio checks.

You may want to just recopy and paste the code.

Aug 02, 2012 at 07:12 PM Gamemaster

thanks for your help but when i add in the new code it comes up with more errors :/, "Random does not contain a definition for 'Range' " and #1' cannot convert object' expression to type float' and The best overloaded method match for `UnityEngine.Vector3.Vector3(float, float, float)' has some invalid arguments im really sorry for this trouble, i am in the process of learning c# and i still need some help...

Aug 03, 2012 at 08:55 AM jazza

That's strange. I copied and pasted and tested with no errors. What is the target platform set to? Make sure the file is named Teleporter.cs, Make sure you have using UnityEngine, Make sure you don't have any scripts named Random. Go through the code line by line to make sure nothing was cut off in the copy.

Aug 03, 2012 at 06:57 PM Gamemaster
(comments are locked)
10|3000 characters needed characters left

thank you very much I will try to learn C#, but till then you have helped me a heap.

more ▼

answered Jul 23, 2012 at 06:48 PM

TheCradam gravatar image

TheCradam
-4 4 4 4

You should convert this to a comment.

Jul 23, 2012 at 06:54 PM Bovine
(comments are locked)
10|3000 characters needed characters left

Slender was created in unity just to let you know. I would start off by writing some scripts where if you collide with the page create a GUI.Label (not sure if its the same in c#.) but yah.Good luck to you.

more ▼

answered Feb 18, 2013 at 12:48 AM

Garrett Schuetz gravatar image

Garrett Schuetz
0

I wouldn't use GUI. Anything, even on PC and especially not on mobile

Feb 18, 2013 at 06:31 AM Bovine
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x9278
x3655
x1702
x296

asked: Jul 20, 2012 at 10:18 AM

Seen: 11741 times

Last Updated: Feb 18, 2013 at 06:38 AM