Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D Sprite Follow Mouse

Discussion in '2D' started by llorecoding, Jul 22, 2015.

  1. llorecoding

    llorecoding

    Joined:
    Jul 22, 2015
    Posts:
    9
    Hello,

    I already looked around to see if there was any post about this question. All I found we're (2D Sprite Angle, Follow In A Line, etc..) questions that was not for my topic. I am wondering how to make a 2D sprite follow my mouse cursor. I think it has something to do with "transform.position", but I don't know. I also, use c# for my coding.

    Thanks.
     
  2. Zk

    Zk

    Joined:
    May 25, 2013
    Posts:
    19
    Basically you're going to want to sample the mouse position and then move your object towards it. I think the following should work.

    This code should grab the mouse position and store it in a Vector3 called positionToMoveTo. You need to access the camera to convert the camera view point to the point in the world. I'm also setting Z to 0 since we're using 2D, but you could use whatever you want. You'd probably want to use the same Z as the object that's following, so it doesn't change.

    Code (CSharp):
    1. Vector3 positionToMoveTo = theCamera.ScreenToWorldPoint(Input.mousePosition);
    2. positionToMoveTo.z = 0;
    Here's code to move an object called "objectToMove" towards a Vector3 called positionToMoveTo at a speed moveSpeed.

    Code (CSharp):
    1. objectToMove.transform.position = Vector3.MoveTowards(objectToMove.transform.position, positionToMoveTo, moveSpeed * Time.deltaTime);
     
    llorecoding likes this.
  3. llorecoding

    llorecoding

    Joined:
    Jul 22, 2015
    Posts:
    9
    Thank you. I typed all the code in and I received error.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseMove : MonoBehaviour {
    5.  
    6.     public Camera myCamera;
    7.     public Sprite mySprite;
    8.     public float moveSpeed = 2.0f;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.    
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.         Vector3 positionMoveTo = myCamera.ScreenToWorldPoint (Input.mousePosition);
    18.         positionMoveTo.z = 0;
    19.         mySprite.transform.position = Vector3.MoveTowards (mySprite.transform.position, positionMoveTo, moveSpeed * Time.deltaTime);
    20.     }
    21. }
    Then in the console it says:
     
  4. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Sprite doesn't have a transform member because it is neither a game object nor a component. It is just a wrapper for a texture. Use just "transform.position" to move the current game object.
     
    llorecoding likes this.
  5. llorecoding

    llorecoding

    Joined:
    Jul 22, 2015
    Posts:
    9
    Thank you! I had my camera's projection on "perspective", which made the sprite not move. Is there anyway to make it move on "perspective"? If not, it's not big deal anyways.