Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Noura · Nov 29, 2014 at 08:26 AM · c#rayraysray-casting

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..

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image koray1396 · Nov 29, 2014 at 08:33 AM 1
Share

steps;

  • define a speed for the player.

  • calculate the difference vector; touchPosition - playerPosition

  • move the player by speed * differenceVector.normalized

avatar image Noura · Nov 29, 2014 at 02:23 PM 0
Share

How can I do your steps in c# ?!! Explain please ..

Thank you ..

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Noura · Nov 29, 2014 at 10:50 AM 0
Share

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 :)

avatar image Noura · Nov 29, 2014 at 10:59 AM 0
Share

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 ; 
 
avatar image Fornoreason1000 · Nov 30, 2014 at 07:25 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges