Ok, I am beginner in unity, I'm trying to make a weeping angel game. I have a script that allows the enemy to move when i look away and it totally works fine. I then continued on to make a blinking animation (two cubes closing together to block the view of the camera). It also works but the weeping angel stays still when i do that blinking animation. how can I make the enemy move the moment the animation occurs. That is my only problem, the enemy does not move when the blinking occurs.
Here is the enemy move when player looks away script.(javascript)
var target : Transform;
//Checks his position, used to follow the target
var pos : Transform;
//Ray variables (Length... etc.)
var rayLength : float;
//Movement, speed etc.
var speed : float;
//You can move if he is not being looked at
var move : boolean = false;
//Sets the sound effect trigger to be false at start
var soundTrigger : boolean = false;
//Defines your sound effect
public var Sound : AudioClip;
//Defines whether the sound has been played before.
private var HasPlayed : boolean = false;
//I had problems with my model sinking into the floor, adjust this variable if you need it, or remove it
/*function FixedUpdate()
{
transform.position.y = 30;
transform.rotation.x = 0;
transform.rotation.z = 0;
}*/
function Update()
{
//Setting up Raycast variables for simple object avoidance
var fwd = transform.TransformDirection (Vector3.forward);
var hit : RaycastHit;
//If you are looking at the object...
if (renderer.isVisible)
{
move = false;
soundTrigger = true;
}
//If you are NOT looking at the object...
if(!renderer.isVisible)
{
move = true;
//resets sound variables
HasPlayed = false;
soundTrigger = false;
}
//If you are not looking at the object...
if(move)
{
//Make him look at the target
transform.LookAt(target);
//Always follow the target
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
//Sets the sound effect componnet. If the player is looking, the sound will play.
if (soundTrigger)
{
//Make sure the sound effect has not been played before. (Without this, this tends to lead to a repeated playing of the sound effect with every frame
if(!HasPlayed)
{
audio.PlayOneShot(Sound);
//Sets boolean to define that the above sound has been play already, and does not need to be played again while the player is looking at the target.
HasPlayed = true;
}
}
//If he is 3 units away from something, move right (Works if you are not looking at the object)
if (Physics.Raycast (transform.position, fwd, rayLength) && move)
{
Debug.Log("Something ahead, moving");
transform.Translate(Vector3.right * 3 * Time.deltaTime);
}
}
↧