How i can make to a entity turn to face player

Posted By: LWD

How i can make to a entity turn to face player - 01/05/07 17:41

Hi!
I am trying to make this, but without success... How i can make to a entity turn to face player?
I´m using A6 Template.
Thanks!
Posted By: JackTheRipper

Re: How i can make to a entity turn to face player - 01/05/07 17:50

Hi!

You can put :
Code:
  
vec_set(temp, player.x);
vec_sub(temp, my.x);
vec_to_angle(my.pan, temp);



into your action!
but make sure your player is the player entity!

if the player isent the player put :
Code:
 
entity* player;

action ...
{
...
player = me;
...
}



into the player action!

ok... hope this helps!

-jack
Posted By: LWD

Re: How i can make to a entity turn to face player - 01/05/07 18:14

thanks! This work, but the entity just turn to face player one time, and after he stay at the same position, how i can make to the entity turn to face player all time?
Thanks!
Posted By: aztec

Re: How i can make to a entity turn to face player - 01/05/07 18:43

put it in a while loop
Posted By: JackTheRipper

Re: How i can make to a entity turn to face player - 01/05/07 18:45

yeah i forgot it in a while-loop!

while(1)
{
vec_set(temp, player.x);
vec_sub(temp, my.x);
vec_to_angle(my.pan, temp);
wait(1);
}

thx aztec!

-jack
Posted By: aztec

Re: How i can make to a entity turn to face player - 01/05/07 18:50

never mind
Posted By: LWD

Re: How i can make to a entity turn to face player - 01/05/07 18:51

Thanks!
I make than the entity turn to face the player when he press the key_enter, but the entity turn very fast, how i can make to the entity turn slow?
Thanks a lot!
Posted By: aztec

Re: How i can make to a entity turn to face player - 01/05/07 18:58

Code:
if (vec_dist (player.x, my.x) < 100) // the player has come clos
{
if (key_e == on)
{
while(1)
{
vec_set(temp, player.x);
vec_sub(temp, my.x);
vec_to_angle(my.pan, temp);
wait(1);
}
}
}



now he turns if the player presses e just change e to enter
Posted By: LWD

Re: How i can make to a entity turn to face player - 01/05/07 19:14

Thanks! But this code dont work :/, but if i remove the line if (key_e == on)the code works, but the entity turns without i press any key...
Thanks!
Posted By: JackTheRipper

Re: How i can make to a entity turn to face player - 01/05/07 19:20

you must set all in a while-loop and it should work fine!
hope this helps!

-jack
Posted By: nipx

Re: How i can make to a entity turn to face player - 01/05/07 19:37

instead of if(key_e==on) place if(key_enter==on)...
If I understood you rigt...



nipx
Posted By: LWD

Re: How i can make to a entity turn to face player - 01/05/07 19:43

It works fine, thanks to all! But when i press key_e the entity turns very fast, have a way to change de speed that the entity rotate?
Thanks!
Posted By: nipx

Re: How i can make to a entity turn to face player - 01/05/07 20:05

With vec_to_angle(my.pan, temp); you set the new angle (pan) directly. If you want to turn it use c_rotate.


--->>

Save the new angle.
Code:

//create new vector to store angles
var vecAngle[3]

-> vec_to_angle(vecAngle, temp);


then calculate the difference between the angle the enemy has now and the one he should turn to.

Code:

//get diff between current angle and the needed
vec_diff(temp, vecAngle, my.pan);



then turn your enemy

Code:

c_rotate(me, temp, glide);




I didnt test it but it should work.



nipx
Posted By: LWD

Re: How i can make to a entity turn to face player - 01/05/07 20:19

I did not understand...
How i can apply this code to the old code?
I substitute the old code:
vec_set(temp, player.x);
vec_sub(temp, my.x);
vec_to_angle(my.pan, temp);
for the new:
var vecAngle[3]
vec_diff(temp, vecAngle, my.pan);
c_rotate(me, temp, glide);

and the entity turns to left, and not to the player...
Thanks!
Posted By: nipx

Re: How i can make to a entity turn to face player - 01/05/07 20:28

Code:

var vecAngle[3];
vec_set(temp, player.x);
vec_sub(temp, my.x);
vec_to_angle(vecAngle, temp);
vec_diff(temp, vecAngle, my.pan);
c_rotate(me, temp, glide);




nipx
Posted By: LWD

Re: How i can make to a entity turn to face player - 01/05/07 20:44

thanks! this works, but the entity rotate very fast too... How i change the speed of the rotation? Thanks a lot!
Posted By: nipx

Re: How i can make to a entity turn to face player - 01/05/07 21:06

Code:

var vecAngle[3];
vec_set(temp, player.x);
vec_sub(temp, my.x);
vec_to_angle(vecAngle, temp);
vec_diff(temp, vecAngle, my.pan);

var Scale =0.25;//(play with 0.25...)
var counter=0;
var tmpCounter;

//scale vector, make it slower
vec_scale(temp,Scale);

//calculate how many times to rotate when vector is scaled
tmpCounter= 1 /Scale; //when using 0.25 -> 4


while(counter < tmpCounter)
{
c_rotate(me, temp, glide);
counter+=1;
wait(1);
}




dont know if this will work but I hope


nipx
Posted By: LWD

Re: How i can make to a entity turn to face player - 01/05/07 21:26

Thanks a lot! I just made a change in the order of the lines of you code and it works fine!
Thanhs to all!!!
Posted By: nipx

Re: How i can make to a entity turn to face player - 01/05/07 21:44

try this:

Code:


//scale vector, make it slower
vec_scale(temp,(Scale *time_step));

//calculate how many times to rotate when vector is scaled
tmpCounter= Scale *time_step ;

while(counter < tmpCounter)
{
c_rotate(me, temp, glide);
counter+=1;
wait(1);
}





edit: if it works now you still should include time_step in your code otherwise the rotation wont be smooth
Posted By: LWD

Re: How i can make to a entity turn to face player - 01/06/07 01:08

Thanks! This code work fine too... but the codes that you have posted have just a problem, when i try to walk around the entity, some times he have a "bug", he turns to the wrong side. example: i go to the left of the entity and the entity rotate to right to go for the left... Have a way to fix this bug?
I hope that you have understood me.
Thanks!
Posted By: Futurulus

Re: How i can make to a entity turn to face player - 01/06/07 03:53

The angles might be straddling 0 and 360. Try adding
Code:
temp.pan = ang(temp.pan);  // EDIT: I put x instead of pan... shouldn't matter though :P
temp.tilt = ang(temp.tilt);


right after you scale temp. This changes it so the angles are -180 to 180, so he'll turn in the right direction.
Posted By: LWD

Re: How i can make to a entity turn to face player - 01/06/07 04:40

Thanks a lot, this code works fine!
Just one more question, when i jump the entity's face go up with me, how i can make to the entity dont move the face for up or down?
Thanks to all for the help!
Posted By: LWD

Re: How i can make to a entity turn to face player - 01/08/07 20:26

I already fix the problem, thanks a lot to all!!!
© 2024 lite-C Forums