# Is Avatar Facing Target?

Please take a moment to consider if this thread is worth bumping.

## Recommended Posts

Hello! Old SL user, but was away for a long time! I hadn't used SL since around the time that shadows were made more dynamic, and came back to many wonderful changes! I'm working on a script for wands, and for the particle effect to look right for some spells, the avatar needs to be facing its target. I'm sure there's some way of doing that by checking the rotation of the avatar to the relativity of the target, but I don't know the logic to make that happen. Of course I'm sure that wouldn't work if the avatar is sitting, say on a pose ball, and then of course animations cause a whole different problem with that. My goal in developing the wand, is to also create an animation, possibly for each spell. Animations aside, assuming the pose balls used are for dueling, in which case, I would assume would be facing each other, I would also want the script to check if the avatar is sitting, and if not, it would check to make sure it is facing the target. Sorry to ramble, I've been up all day and had too much coffee.

##### Share on other sites

Welcome back, Ruthven!

This is the calculation you ask for:

`		key id;//target's uuid		vector vStart = llGetPos()+<1.0,0.0,0.0>*llGetRot();//my positive x axis		vector vEnd = llList2Vector(llGetObjectDetails(id,[OBJECT_POS]),0);		rotation rot = llRotBetween(vStart,vEnd);//the angle between my positive x axis and the target		llOwnerSay((string)rot);		vector vec = llRot2Euler(rot)*RAD_TO_DEG;		llOwnerSay((string)vec.x); //degrees between the direction in which I'm facing and target position`

However, if vec.x is not 0.0 -- that is, you're not looking straight at your target -- you don't know whether you're going to hit  it or not.   That depends on distance between you and the target, and is not easy to calculate.

So I would suggest using llCastRay instead.

If you decide the maximum range for your spell is 20.0 metres, you would do a ray-cast between llGetPos() and llGetPos()+<20.0,0.0,0.0>*llGetRot() (20 metres on your positive x axis) and see what, if anything, you hit.

That's how I would do it, anyway.

##### Share on other sites

You can get a rough idea of which way an avatar is facing if you detect it with a sensor and then ask about llDetectedRot(0).  That's really only a rough indication, because it doesn't know about any apparent rotations that may be due to animations -- all of which are client-side.

You can tell whether the avatar is sitting by using its UUID and asking for llGetAgentInfo and checking AGENT_SITTING

##### Share on other sites

`vector avPos = llGetPos();rotation avRot = llGetRot(); // some of the previous posts deal with the avatar rotation which is worth some considerationsvector targetPos; // find this with sensor or whatever// Now compute two unit vectorsvector facing = llRot2Fwd( avRot);vector target = llVecNorm( targetPos - avPos);// finaly compute the dot product of both vectorsfloat dp = facing*target; // ( this is the same as cosinus to the angle between the vectors!! look it up:) )if ( dp == 1.00 ) // you are facing target exactly// since vi operate in floats and want something not perfct to be accepted, use something smaller than one// like:if ( dp >= 0.95 ) // you are facing target`

:smileysurprised::smileyvery-happy:

##### Share on other sites

Thanks! I think this might be what I'm looking for, a "close enough" rotation, not an exact facing like one of the other suggestions. I will give it a try.

##### Share on other sites

Thanks! I'm not worried too much about an exact facing, I just don't want the particles coming out and moving in too much of a curve, or back through my avatar,  if my avatar is facing to much away from the target (whether it be an object or an avatar). I didn't know about the llCastRay function though, and can see where it might be usefull for some other ideas that I have.

##### Share on other sites

The value of dp will always be in the range -1.0 ≤ dp ≤ 1.0

where 1 is facing, -1 is facing away and 0 is 'side to target'

:smileysurprised::smileyvery-happy: