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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Lirra · Jun 11, 2015 at 10:43 AM · 2dspriteobjectfollowmouseposition

2D how to make a sprite follow the mouse position #c

I've started programming with Unity not long ago and I have a noob question:

The sprite should follow the mouse and should have the same position all the time. When I first try writing the code I came across a little problem: The sprite moves outside the camera area, although the mouse is inside.

In the game I'm programming the mouse should be able to drag an drop object from one point to another. Simiar to a dress-up game.

Thanks for reading.

This is my code:

using UnityEngine; using System.Collections;

public class followTheMouse : MonoBehaviour {

 Rigidbody2D rigid;

 // Use this for initialization
 void Start () {

     rigid = GetComponent<Rigidbody2D> ();
 
 }
 

// Update is called once per frame void Update () {

     float mouseX = Input.mousePosition.x;
 float mouseY = Input.mousePosition.y;
 Vector2 followthecursor = new Vector2 (mouseX, mouseY);

     rigid.MovePosition ((Vector2)transform.position +(Vector2)followthecursor);

 }

}

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 crohr · Jun 11, 2015 at 04:49 PM

You will want to use Camera.ScreenToWorldPoint to convert the mouse position to world coordinates. http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

Comment
Add comment · Show 4 · 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 Lirra · Jun 11, 2015 at 06:50 PM 0
Share

Oh thank you. This was exactly what was missing.

avatar image crohr · Jun 11, 2015 at 07:30 PM 0
Share

No problem, good luck! Don't forget to accept this answer for future readers.

avatar image ButterKhrp · Jan 29, 2017 at 10:54 PM 0
Share

Hey, i'm trying to make a similar script, but am having no luck. Can you post the full script?

avatar image nebur7420 · Nov 29, 2018 at 09:57 AM 0
Share

$$anonymous$$eep in $$anonymous$$d to reset the Z position to 0. Sometimes it changes to 10 or so.

avatar image
1

Answer by lqvinh2 · Nov 29, 2018 at 09:31 AM

void Update () { // for 2D game Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); mousePos .z = 0; speed = 150; transform.position = Vector3.Lerp(transform.position, mousePos , speed * Time.deltaTime); }

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

6 People are following this question.

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

Related Questions

How do I drag a sprite around? 2 Answers

[2D] Object at X distance to Y direction from point 1 Answer

how to have a sprite follow the spot of my mouse cursor. 0 Answers

2d game sprite frame of object issue 1 Answer

Camera follow 2D sprite when it goes off screen 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