- Home /
Obtain direction from a point relative to another point
First, srry for bad english isnt my primary lenguaje
im having trouble trying to get the direction of a vector 3 point.
i have touchphase and a raycast, so i want to take the point of touch and make my character jump in that direction. Everything is working fine, but my character jump in a diferent direction.
i think it is happen because the direction is based on the 0,0,0 point,
what i need to do to make the direction relative to the current character position?
thanks
Answer by Berenger · May 22, 2012 at 04:25 AM
For the basics, the vector from A to B is B - A. For something more pratical, see aldonaletto's answer.
actually, im making a 2d game, this is why i cant use lookat, i will try substract the points, thanks a lot
this just wor fine, im now hitting my head asking myself why i dont think in this before, thanks a lot :)
I won't lie to you, I'm not really the first to think about vector calculation :p
Answer by Unk · May 22, 2012 at 04:33 AM
Assuming you are in a top down view and you want your character to move/jump in the direction you touched I would first make the character look at the spot touch using transform.LookAt(touchedPosition); and then you can move your character using on his Vector3.forward..
Answer by aldonaletto · May 22, 2012 at 04:01 AM
The question is too vague - you should post your script.
Anyway, in most cases you should do something like this:
function Update (){ if (Input.touchCount > 0){ var touch: Touch = Input.touches[0]; if (touch.phase == TouchPhase.Began){ var ray = Camera.main.ScreenPointToRay (touch.position); // get a ray from the touch var hit: RaycastHit; if (Physics.Raycast(ray, hit)){ // if the ray hits something... // hit.point contains the 3D point clicked // jump to this point } } } }
i didnt post the code, because i dont need that stuff, mi code is ok, i only need the method to obtain the correct direction. Thanks anyway