Hi all,
I have been working on a slender like game and I have come across a problem, my enemy seems to fly. This is not my script. Also the enemy teleports way outside the constraints I have assigned to him. There are three scripts attached to him, BasicSlenderTeleport, AI and EnemyScript. You may know these from the Slender guide by alucardj. Perhaps you could tell me the values I must assign to him in order for him to teleport in the area in that I have linked a picture to bellow. The map is 2000 x 2000
Here are the scripts :
AI
/// created by MasterDevelopers ///
#pragma strict
var target : Transform;
var rotSpeed = 3;
var myTransform : Transform;
function Awake()
{
myTransform = transform;
}
function Start ()
{
target = GameObject.FindWithTag("Player").transform;
}
function Update ()
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position - myTransform.position),rotSpeed * Time.deltaTime);
}
BasicSlenderTeleport
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 ()
{
yield WaitForSeconds (141.2);
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.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;
}
}
EnemyScript
/// created by MasterDevelopers ///
#pragma strict
@script RequireComponent( AudioSource )
public var thePlayer : Transform;
private var theEnemy : Transform;
public var speed : float = 5.0;
var isOffScreen : boolean = false;
public var offscreenDotRange : float = 0.7;
var isVisible : boolean = false;
public var visibleDotRange : float = 0.8; // ** between 0.75 and 0.85 (originally 0.8172719)
var isInRange : boolean = false;
public var followDistance : float = 24.0;
public var maxVisibleDistance : float = 25.0;
public var reduceDistAmt : float = 1.1;
private var sqrDist : float = 0.0;
public var health : float = 100.0;
public var damage : float = 20.0;
public var enemySightedSFX : AudioClip;
private var hasPlayedSeenSound : boolean = false;
private var colDist : float = 5.0; // raycast distance in front of enemy when checking for obstacles
function Start()
{
yield WaitForSeconds (141.2);
if ( thePlayer == null )
{
thePlayer = GameObject.Find( "Player" ).transform;
}
theEnemy = transform;
}
function Update()
{
// Movement : check if out-of-view, then move
CheckIfOffScreen();
// if is Off Screen, move
if ( isOffScreen )
{
MoveEnemy();
// restore health
RestoreHealth();
}
else
{
// check if Player is seen
CheckIfVisible();
if ( isVisible )
{
// deduct health
DeductHealth();
// stop moving
StopEnemy();
// play sound only when the Man is first sighted
if ( !hasPlayedSeenSound )
{
audio.PlayClipAtPoint( enemySightedSFX, thePlayer.position );
}
hasPlayedSeenSound = true; // sound has now played
}
else
{
// check max range
CheckMaxVisibleRange();
// if far away then move, else stop
if ( !isInRange )
{
MoveEnemy();
}
else
{
StopEnemy();
}
// reset hasPlayedSeenSound for next time isVisible first occurs
hasPlayedSeenSound = false;
}
}
}
function DeductHealth()
{
// deduct health
health -= damage * Time.deltaTime;
// check if no health left
if ( health <= 0.0 )
{
health = 0.0;
Debug.Log( "YOU ARE OUT OF HEALTH !" );
// Restart game here!
Application.LoadLevel( "rentry" );
}
}
function RestoreHealth()
{
// deduct health
health += damage * Time.deltaTime;
// check if no health left
if ( health >= 100.0 )
{
health = 100.0;
//Debug.Log( "HEALTH is FULL" );
}
}
function CheckIfOffScreen()
{
var fwd : Vector3 = thePlayer.forward.normalized;
var other : Vector3 = (theEnemy.position - thePlayer.position).normalized;
var theProduct : float = Vector3.Dot( fwd, other );
if ( theProduct < offscreenDotRange )
{
isOffScreen = true;
}
else
{
isOffScreen = false;
}
}
function MoveEnemy()
{
// Check the Follow Distance
CheckDistance();
// if not too close, move
if ( !isInRange )
{
rigidbody.velocity = Vector3( 0, rigidbody.velocity.y, 0 ); // maintain gravity
// --
// Old Movement
//transform.LookAt( thePlayer );
//transform.position += transform.forward * speed * Time.deltaTime;
// --
// New Movement - with obstacle avoidance
var dir : Vector3 = ( thePlayer.position - theEnemy.position ).normalized;
var hit : RaycastHit;
if ( Physics.Raycast( theEnemy.position, theEnemy.forward, hit, colDist ) )
{
//Debug.Log( " obstacle ray hit " + hit.collider.gameObject.name );
if ( hit.collider.gameObject.name != "Player" && hit.collider.gameObject.name != "Terrain" )
{
dir += hit.normal * 20;
}
}
var rot : Quaternion = Quaternion.LookRotation( dir );
theEnemy.rotation = Quaternion.Slerp( theEnemy.rotation, rot, Time.deltaTime );
theEnemy.position += theEnemy.forward * speed * Time.deltaTime;
//theEnemy.rigidbody.velocity = theEnemy.forward * speed; // Not Working
// --
}
else
{
StopEnemy();
}
}
function StopEnemy()
{
transform.LookAt( thePlayer );
rigidbody.velocity = Vector3.zero;
}
function CheckIfVisible()
{
var fwd : Vector3 = thePlayer.forward.normalized;
var other : Vector3 = ( theEnemy.position - thePlayer.position ).normalized;
var theProduct : float = Vector3.Dot( fwd, other );
if ( theProduct > visibleDotRange )
{
// Check the Max Distance
CheckMaxVisibleRange();
if ( isInRange )
{
// Linecast to check for occlusion
var hit : RaycastHit;
if ( Physics.Linecast( theEnemy.position + (Vector3.up * 1.75) + theEnemy.forward, thePlayer.position, hit ) )
{
Debug.Log( "Enemy sees " + hit.collider.gameObject.name );
if ( hit.collider.gameObject.name == "Player" )
{
isVisible = true;
}
}
}
else
{
isVisible = false;
}
}
else
{
isVisible = false;
}
}
function CheckDistance()
{
var sqrDist : float = (theEnemy.position - thePlayer.position).sqrMagnitude;
var sqrFollowDist : float = followDistance * followDistance;
if ( sqrDist < sqrFollowDist )
{
isInRange = true;
}
else
{
isInRange = false;
}
}
function ReduceDistance()
{
followDistance -= reduceDistAmt;
}
function CheckMaxVisibleRange()
{
var sqrDist : float = (theEnemy.position - thePlayer.position).sqrMagnitude;
var sqrMaxDist : float = maxVisibleDistance * maxVisibleDistance;
if ( sqrDist < sqrMaxDist )
{
isInRange = true;
}
else
{
isInRange = false;
}
}
Heres the image of the attached components:
![alt text][1]
![alt text][2]
Heres what where I want him to teleport to :
http://www.mediafire.com/convkey/0281/z6vsfvrrvds00lmzg.jpg
[1]: /storage/temp/58166-components2.png
[2]: /storage/temp/58167-components1.png
**I have also exported the scene as an object here it is : ** https://www.mediafire.com/?6frdqd8cwoseead
Apologies for the long question,
Thanks,
Eoin
↧