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 Dawnk41 · Apr 22, 2014 at 04:38 PM · mouseposition

Trying to move an object to Mouse x and z positions only?

Hi, I'm trying to move an object in the game to the Mouse x and z coordinates whenever I right-click, but not to the Y coordinate. Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class Mousetargeting : MonoBehaviour
 {    void Update ()
     {
         if(Input.GetMouseButtonDown(1))
         {
             transform.position = new Vector3(mousePosition.x, -.45, mousePosition.z);
         }
     }
 }

I'm not sure how to just call up the mouse X and Z coordinates when I'm transforming the position. Help?

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

2 Replies

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

Answer by robertbu · Apr 23, 2014 at 08:12 AM

Your question lacks details for a solid answer. While Input.mousePosition is a Vector3, the 'z' is 0.0, so there is no inherit 'z' to mousePosition. Depending on the rotation of the camera and the nature of the game, there are a variety of ways of placing an object in the scene. As @KevLoughrey mentions, you can use Camera.ScreenToWorldPoint(). If we assume the rotation of the camera is (0,0,0) with your object in front of the camera, then the 'z' position will be at some specific distance in front of the camera. So your code might look like:

 void Update ()
 {
    if(Input.GetMouseButtonDown(1))
    {
        float dist = transform.position.z - Camera.main.transform.position.z;
        Vector3 pos = Input.mousePosition;
        pos.z = dist;
        pos = Camera.main.ScreenToWorldPoint(pos);
        pos.y = transform.position.y;
        transform.position = pos;
    }
 }

So we first calculate the distance between our object and the camera on the 'z' axis. Then we create a Vector3 composed of the mousePosition and the distance as the 'z' component. ScreenToWorldPoint() converts that Vector3 into a world position. Since you did not want the 'y' component, we overwrite the 'y' component with the current one of the object, and finally assign the result to 'transform.position'.

Comment
Add comment · Show 1 · 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 MFKJ · Apr 21, 2016 at 06:35 AM 0
Share

Lovely work Thank you so much @robertbu

avatar image
0

Answer by KevLoughrey · Apr 22, 2014 at 04:40 PM

 Input.mousePosition 
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 Dawnk41 · Apr 22, 2014 at 11:07 PM 0
Share

Yes, but how do you make it only return the X and Z coordinates, and how do I assign those values only to the instance I'm moving?

avatar image KevLoughrey · Apr 23, 2014 at 07:50 AM 0
Share

Input.mousePosition returns a vector3, so there's no way to only return the x and z values. You had the right idea above, and should use something like:

transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0)

If that's not working for what you need, then you might need to convert the mouse's screen position into gameworld coodinates, using Camera.main.ScreenToWorldPoint(Input.mousePosition), but you will need to modify the z value of mousePosition first or it won't work. If you do a quick google search, you can find a bunch of threads here about that.

avatar image Hoeloe · Apr 23, 2014 at 08:02 AM 0
Share

There are two problems with you code.

As $$anonymous$$evLoughrey has said, Input.mousePosition is what you need to use ins$$anonymous$$d of just mousePosition. If that doesn't work, then ScreenToWorldPoint is a good idea.

The other issue is that you use -.45 as the third value. This will throw an error. In C#, there are different kinds of number. The main ones you will use are int and float. The difference is that int supports only integer values, and float supports decimal values. However, there is another type, double, which supports decimal values to greater precision than float. The two types are not the same, so C# requires that you deter$$anonymous$$e which you are using at any given time. By default, decimal values are converted to the double type, but Unity's Vector3 class uses float. In order to specify that a value is a float and not a double, postfix it with an f.

So, your -.45 should be -.45f.

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

23 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

Related Questions

How can I stop getting mouse position of Ui Elements? 0 Answers

Shake problem when colliding to walls Top Down (via MousePos) 0 Answers

Mouse aims at object ? 1 Answer

Get the length from the object i click on to the mouserelease.. how? 0 Answers

Scale a 2D object based on mousePosition on click 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