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 Mtts · Feb 19, 2013 at 01:29 PM · objectmousefollow

Object where the mouse is

Hi all,

Im trying to make an 2D breakout game where I use the mouse in X and Y-position. All tutorials i've found uses the keyboard.

Is there anyone that maybe can help or get me going with the script?

Sorry if its a newbiequestion.

Comment
Add comment · Show 1
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 AlucardJay · Feb 19, 2013 at 04:03 PM 0
Share

I read this question as saying How can I calculate and use the changes in mouse movement for Input X and Y values (like Touch.deltaPosition but for mouse). Is this what you mean? If so, check my answer here : http://answers.unity3d.com/questions/332013/calculating-change-in-mouse-position.html

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Melanina · Feb 19, 2013 at 02:45 PM

this should be what you want.

    gameObject.transform.position = Camera.main.ScreenToWorldPoint ( Input.mousePosition );
 
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 arkon · Feb 19, 2013 at 01:49 PM

Try using Input.mousePosition, it is a vector3 of the mouse position.

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 Professor Snake · Feb 19, 2013 at 01:54 PM

You can use a raycast that originates from the camera, whose direction is the mouse's position:

 var hit:RaycastHit;
 var mask:LayerMask;
 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
       
         if (Physics.Raycast (ray,hit, 500,mask)) {
           //do something at hit.point, which is the point at which the raycast hit a collider
         }
       

EDIT: I apologise, i misunderstood your question. I will keep my answer up however in case it helps.

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 robertbu · Feb 19, 2013 at 03:57 PM

Both @Professor Snake and @Melanina solutions will work, but what they they wrote will only take you half way. For using ScreenToWorldPoint() you need to set the Z parameter to the distance in front of the camera you want to find. If your camera is looking at positive Z, this code would be attached to the paddle (Untested):

 void Update() {
     Vector3 v3Pos = Input.mousePosition;
     // Get distance the paddle is in front of the camera
     v3Pos.z = Mathf.Abs(transform.position.z - Camera.main.transform.position.z);
     v3Pos = Camera.main.ScreenToWorldPoint (v3Pos);  
     // Don't want to move the paddle up and down, just x 
     v3Pos.y = transform.position.y;
     transform.position = v3Pos;
 }

For @Professor Snake's solution, you would need to construct a mathematical plane at the distance the paddle is from the camera. Then you would use Plane.Raycast() with the ray he constructed to find the screen point in world space.

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

13 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

Related Questions

Mouse Aiming Follow Wrong Direction - Help 2 Answers

Need Help: Unity draw-to-follow? 0 Answers

Run script when object is clicked 1 Answer

Make the flashlight follow the mouse. 1 Answer

Mouse Orbit script following other object 0 Answers


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