- Home /
move player to finger position
Hello,
I made this code to make the player look at finger point if the user touch the screen ..
Void LookAtFinger () { Ray ray = cam.ScreenToRay (Input.mousePosition) ;
RayCastHit hit ;
if (physics. RayCast (ray, out hit , Mathf.Infinity , detectorLayer ) { transform.LookAt (hit.point); transform.eularAngles = new vector3 (-90 , transform.eularAngles.y , 0 ) } }
But I want the player to move also when he look to the finger point to move to the position that he touched..
What can I use ?!! What is the steps that I have to do?!!
Thank you..
steps;
define a speed for the player.
calculate the difference vector; touchPosition - playerPosition
move the player by speed * differenceVector.normalized
How can I do your steps in c# ?!! Explain please ..
Thank you ..
Answer by Fornoreason1000 · Nov 29, 2014 at 08:48 AM
first please format your code next time, use the 1010 button to do so... sometimes it doesn't work though se be sure to check that it works
basically when the user touches the screen the character will look at then proceed to walk to that position? the anwser is pretty simple
lets look how you got the player to look in that direction
void LookAtFinger () {
Ray ray = cam.ScreenToRay (Input.mousePosition) ;
RayCastHit hit ;
if (physics. RayCast (ray, out hit , Mathf.Infinity , detectorLayer ) {
transform.LookAt (hit.point);
transform.eularAngles = new vector3 (-90 , transform.eularAngles.y , 0 ) } }
here you get your screen coordinates for the user input then ray cast until you hit something, then rotate the character look at that point. this point we will call dest
now we got the direction of the dest, because... as you said the player is looking at it. so when we move the player in its forward direction , it will eventually reach it...
so all we do is once we have out direction move it forward
transform.position += Vector3.forward * transform.rotation * Time.deltaTime;
now your character should continue to move forward until it reaches dest, then we need to make sure it will stop at dest (and not spin around or something). so we need to check if the player is at dest, if so stop moving, if not coninue
var dest = hit.point;
if(Vector3.distance(dest,transform.position) > 0.0f) {
transform.position += Vector3.forward * transform.rotation * Time.deltaTime;
}
there we go... your character should now move to where ever the user has there finger on the screen
now there a'lot of ways you might use to actually move your character (e.g controller.Move), I'm just editing the position directly for simplicity and explanatory reasons. it doesn't matter which you use.
Hope it helps
Thank you for your help I tried your code It's worked but the player keep moving forward If I touch another place in the screen the player will just look at finger and keep moving forward he didn't come to the position that I touched I hope you understand what I mean and I will be waiting for your answer I want the player to move to the position to the finger point
Thanks a lot :)
Sorry I forgot something to tell you
When I added this line it give me an error
The error Operator * cannot applied to operands of type unityEngine.vector 3 and UnityEngine .Quarternion
But if delete transform.rotate the error disappear
transform.position += vector3 .forward * transform.rotation * time. DeltaTime ;
oops... see that's my fault.. I'm not sure if you know already but Quaternion's and vector3's must be multiplied in a certain order to get a real result. it's really advanced math stuff on why that happens and unity prevents it by not allowing them to be in the wrong order hence you get that error... as far as i know it only applies to $$anonymous$$utiplication of a Vector3 and a Quaternion
read this for more info: http://answers.unity3d.com/questions/372371/multiply-quaternion-by-vector3-how-is-done.html
anyway the correct code would be:
transform.position += transform.rotation * Vector3.forward * Time. DeltaTime ;
//always make sure you have the Quaternion before the Vector when multiplying
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Raycasting ignoring small sized object 1 Answer
What is the best way to send raycasts out in a sphere? And what resolution do I need to be accurate? 2 Answers
Why in Unity3D RaycastHit. textureCoord always return 0,0 after building the project? 1 Answer