Answers

Slender like game

0

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.

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.

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

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.

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;

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.

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

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.

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.