Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Mixi777 · Aug 22, 2017 at 09:50 PM · cameraplayer movementissuescreentoworldpoint

Converting mouse position to worldpoint in 3d

I had this script for moving player in my 2d game:

void Update () {

     if (Input.GetMouseButton(1))
     {
         mousePos = Input.mousePosition;            
         mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         force = Vector2.ClampMagnitude(new Vector2((mousePos.x - transform.position.x), (mousePos.y - transform.position.y)), maxControlRadius);
         PlayerRB.velocity = force * speed;

} }

But now i want to upgrade to a 3d project. The problem is that i just dont know how to get the right translation from screen to world point coordinates. I have camera above the player looking down at him. But when i try to do this:

Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane));

it always giving me coordinates (-0.1, 0.97 , 3.0). I mentioned that they are similiar to camera position (0, 10, 3).

The thing is that i want to move player only in the X and Z axis. What am i doing wrong? Pleeeeease help me.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Cornelis-de-Jager · Aug 22, 2017 at 10:41 PM

Hi Mixi777,

It looks like you are only working with the positioning vectors instead of directional vectors. What you have to do is find the point you click and the point your transform is at. Then minus the player position from the target location to get direction.

That will give you a direction. The trick then is to move your player, but you have to decide if it will be in relation to the world or the player. It seems like you are referring to the world. See code below:

  // Cast a ray from screen point
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

 // Save the info
 RaycastHit out;

 // You successfully hi
 if (Physics.Raycast (ray, out hit)) {

   // Find the direction to move in
   Vector3 dir = hit.point - transform.position;
 
   // Make it so that its only in x and y axis
   dir.y = 0; // No vertical movement
 
   // Now move your character in world space 
   transform.Translate (dir * Time.DeltaTime * speed, Space.World);
 
   // transform.Translate (dir * Time.DeltaTime * speed); // Try this if it doesn't work
 }


Of course this is just the base function that doesn't take the players rotation into account. If this is indeed a click to move game, then I highly recommend that you check out NavMeshes. It is very simple to implement and quick to learn.

But if it is simply to move to a location then use the code above. Change the translate to your velocity code.

But Like I said, this doesn't take rotation into account. If you want rotation then use the following code:

 // Find the look at location
 Vector3 target = Camera.main.ScreenPointToWorld (Input.mousePosition);
 
 // Make it so that player always looks forward (prevents entire body goign horizontal)
 target.y = transform.position.y + transform.localScale.y;
 
 // Look at the target location
 transform.LookAt (target);




Comment
Add comment · 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
1

Answer by unidad2pete · Aug 22, 2017 at 10:35 PM

 public Camera yourCam;
 
  if(Input.GetMouseButton(0))
              {
                 Ray ray = yourCam.ScreenPointToRay(Input.mousePosition);
                  RaycastHit hit;
                  if(Physics.Raycast(ray, out hit))
                  {
                      Debug.DrawLine(transform.position, hit.point);
  
                  }
 

https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

https://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html

Comment
Add comment · 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
0

Answer by Mixi777 · Sep 04, 2017 at 05:43 AM

Thanks all of you guys. You totaly made my day. I was strugling so hard.

Comment
Add comment · 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

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

104 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help for player rotation and camera rotation 0 Answers

How can I fix this screen wrapping? 1 Answer

ScreenToWorldPoint not working 1 Answer

Moving Player Relative to Camera? 1 Answer

Camera.main.ScreenToWorldPoint not outputting expected results. 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