x


Enemy move error

Hello , I have a script that makes the enemy follow the player and attack him , but the enemy does't move on a straight way , it moves on a curve way and i want him to move on a straight way , this is my code of attacking , I have another code to make the enemy looks on the player if you need it :

var distance;
var lookAtDistance = 15.0;
var attackRange = 10.0;
var Speed = 5.0;
var damping = 6.0;

private var isItAttacking = false;
private var moveDirection : Vector3 = Vector3.zero;
private var controller : CharacterController;

function Start (){
}

function Update (){
	distance = Vector3.Distance(Body.target.position, transform.position);
	controller = GetComponent(CharacterController);
	if(controller.isGrounded)
	{
		moveDirection.z = Speed;
	}
	if(!controller.isGrounded)
	{
		moveDirection.y -= 10;
	}
	moveDirection = transform.TransformDirection(moveDirection); 
    if(distance < lookAtDistance)
    {
    	isItAttacking = false;
    	lookAt ();
    }   
    if(distance < attackRange)
    {
    	attack ();
    }
}

function lookAt ()
{
	var rotation = Quaternion.LookRotation(Body.target.position - transform.position);
	transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}

function attack ()
{
    isItAttacking = true;
    controller.Move(moveDirection * Time.deltaTime);
}

thanks very much

more ▼

asked Dec 07 '11 at 06:37 PM

Subhi gravatar image

Subhi
67 16 20 25

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I suspect that moveDirection.x is causing this weird behaviour: it should be zeroed to give a consistent local direction vector. You could zero it before TransformDirection, but it's simpler to use transform.forward directly, like below:

function Update (){
    distance = Vector3.Distance(Body.target.position, transform.position);
    controller = GetComponent(CharacterController);
    moveDirection = transform.forward * speed; 
    if(!controller.isGrounded)
    {
        moveDirection.y -= 10;
    }
    if(distance < lookAtDistance)
    {
    	isItAttacking = false;
    	lookAt ();
    }   
    if(distance < attackRange)
    {
    	attack ();
    }
}
more ▼

answered Dec 07 '11 at 11:53 PM

aldonaletto gravatar image

aldonaletto
41.4k 16 42 197

thanks very much

Dec 08 '11 at 12:28 PM Subhi
(comments are locked)
10|3000 characters needed characters left

would i be able to take a look at the move script please! i am having some trouble myself

more ▼

answered May 13 '12 at 08:05 PM

spike2192 gravatar image

spike2192
15 3 3 4

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1367
x653
x498
x155
x26

asked: Dec 07 '11 at 06:37 PM

Seen: 560 times

Last Updated: May 13 '12 at 08:05 PM