|
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
(comments are locked)
|
|
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 ();
}
}
thanks very much
Dec 08 '11 at 12:28 PM
Subhi
(comments are locked)
|
|
would i be able to take a look at the move script please! i am having some trouble myself
(comments are locked)
|
