• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheCradam · Jul 20, 2012 at 10:18 AM · gameobjectaudiocustom

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Jul 20, 2012 at 10:53 AM 1
Share

suggest you learn about transform.position !!

avatar image Bovine · Jul 20, 2012 at 11:45 PM 0
Share

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. $$anonymous$$y suggestion would be that you do something a bit simpler as a knowledge gaining exercise. $$anonymous$$aybe something as simple as a two-player version of noughts and crosses.

avatar image Jeffom · Aug 02, 2012 at 02:51 PM 0
Share

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

avatar image Bovine · Aug 02, 2012 at 03:03 PM 0
Share

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

avatar image Starkid225 · Feb 18, 2013 at 06:38 AM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Gamemaster · Jul 21, 2012 at 12:15 AM

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.

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jazza · Aug 02, 2012 at 02:09 PM 0
Share

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??

avatar image Bovine · Aug 02, 2012 at 02:31 PM 0
Share

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()

avatar image Gamemaster · Aug 02, 2012 at 07:12 PM 0
Share

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 <= ins$$anonymous$$d. And some audio checks.

You may want to just recopy and paste the code.

avatar image jazza · Aug 03, 2012 at 08:55 AM 0
Share

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

avatar image Gamemaster · Aug 03, 2012 at 06:57 PM 0
Share

That's strange. I copied and pasted and tested with no errors. What is the target platform set to? $$anonymous$$ake sure the file is named Teleporter.cs, $$anonymous$$ake sure you have using UnityEngine, $$anonymous$$ake 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.

Show more comments
avatar image
0

Answer by TheCradam · Jul 23, 2012 at 06:48 PM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bovine · Jul 23, 2012 at 06:54 PM 1
Share

You should convert this to a comment.

avatar image
-1

Answer by Garrett Schuetz · Feb 18, 2013 at 12:48 AM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bovine · Feb 18, 2013 at 06:31 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a Slender man follow character script 7 Answers

GameObject reacts to audio source 1 Answer

Music works but not when accessed by another script in JavaScript 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Get acess and switch to many audio source in a single Object HELP!! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges