- Home /
How to make an object move towards the mouse pointer on click? [2D]
Total noob question:
So, the control system I'm looking at is this: The camera follows directly above the player's character, and if the player clicks anywhere, the player's character will move towards that position for the duration of the click. I'm very new to the 2D Unity, can anyone help me with this?
Remember that this is 2D, so "up" is the Z axis.
I'm trying to solve the same problem myself. I've got at least part of the way. Are you using sorting layers and sorting orders? They cause a problem with clicks that I should be able to help with. Also was wondering why you have a Z axis if your game is totally 2D? I'm kinda new to Unity as well, so that might be a daft question.
@Romano About the Z axis thing, I was just re$$anonymous$$ding people that X and Y were the planes I need to move on, and not Z.
I DO have a parallaxing background that requires background layers on different Z axi, and a Perspective camera.
This is pretty simple. You can use the Vector3.Lerp function to achieve this. Use raycasting to get the mouse click position or the touch position. Then use the initial and the final position in the lerp function. The initial position being the position that the gameobject is at now and the final position being the click / touch position. You can find the article by The Game Contriver on the same here
$$anonymous$$ove to Touch / Click Position - The Game Contriver
Answer by robertbu · Feb 07, 2014 at 07:06 AM
You are not asking for rotation, so there is no difference between 2D and 3D. This bit of sample code assumes an Orthographic camera.
#pragma strict
public var moveSpeed = 2.0; // Units per second
function Update () {
if (Input.GetMouseButton(0)) {
var targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetPos.z = transform.position.z;
transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
}
}
I'm actually using a Perspective camera, because I have a parallaxing background, on different Z axi, which didn't work with an Orthographic camera.
Will this still work?
And yeah, I don't need the object to rotate. You play as a black hole, so you're basically a circle.
Here is a bit of code for a perspective camera. It assumes the camera has a rotation of (0,0,0):
#pragma strict
public var moveSpeed = 2.0; // Units per second
function Update () {
if (Input.Get$$anonymous$$ouseButton(0)) {
var pos = Input.mousePosition;
pos.z = transform.position.z - Camera.main.transform.position.z;
pos = Camera.main.ScreenToWorldPoint(pos);
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, pos, moveSpeed * Time.deltaTime);
}
}
This solved a similar problem I was having. Thank you :)
I was using @robertbu 's orthographic camera code, but my object becomes invisible after a few seconds, though other things can still interact with it.
Answer by sagivo · May 20, 2014 at 03:27 PM
have a look at Vector2.MoveTowards
transform.position = Vector2.MoveTowards(transform.position, Target.transform.position, Speed*Time.deltaTime);