- Home /
Too subjective and argumentative. We can't walk you through to completion. For new problems, submit a new question.
How can you make an enemy follow you only when look away or blink?
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);
}
}
I have another problem, the enemies can walk straight through the walls. I'm trying to make a level in my game that has the player trapped inside a house, but the enemy just goes through the walls, ins$$anonymous$$d of getting blocked by the walls
Answer by Kiwasi · May 30, 2014 at 12:11 AM
I haven't read through your code in detail, but here is the meta code to solve your problem.
//set a bool to true each time the blinking animation plays
private bool isBlinking;
if (isBlinking){
//Do your move code in here
}
I made this new movement script( I will still use my 1st script, this is just an add on script) It won't work, could you please tell me the problem?
#pragma strict
var target : Transform;
var pos : Transform;
var speed : float;
var blinking : boolean = false;
var move : boolean = false;
var soundTrigger : boolean = false;
public var Sound : AudioClip;
private var HasPlayed : boolean = false;
function Update ()
{
if(Input.Get$$anonymous$$ouseButtonDown(1))
{
blinking = true;
}
else
{
blinking = false;
}
if(blinking == false);
{
move = false;
soundTrigger = true;
}
if(blinking == true);
{
move = true;
soundTrigger = false;
HasPlayed = false;
}
}
With this code blinking will only return true on the frame that the user clicks.
Try adding a timer that lasts the length of the blinking animation before setting blinking to false.
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to update scoreline on a HIT? 1 Answer
Player damage stops working over time 0 Answers
enemy animations 1 Answer
Enemy AI With changing Player 0 Answers