I know this sounds very simple but I’m still really new with script. I’m taking down as much notes as possible to make life easier in the future. All i want to know is how do i make my player move right direction if i click right side of the player. and move left if i click left side of the player.
I’m making a simple 2D games but surprisingly, i had a hard time trying to find a click to move tutorial for 2D. so please help.
First, realize that your mouse cursor doesn’t exist in the same space as your game world. It exists on your screen, which has its own coordinate system.
It’s easy, though, to convert between the two spaces using Camera.ScreenToWorldPoint(). Input a position on the screen, and it will return a position in the game’s world space. Note that the z-value you input is just how many units away from the camera you want the world point to be. Since this is a 2D game, you can leave this at 0; it doesn’t matter.
Now you just need to get your mouse cursor position with Input.mousePosition, which looks something like this:
Vector3 cursorPosInWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition); //Camera.main is the main camera, which is of type Camera.
That line will give you a world point for your mouse and put it in cursorPosInWorld
. You only need to do this when you click, so we can check for Input.GetMouseButtonDown() which returns true in the frame that the mouse button is pressed down, and false otherwise.
Since you’re interested in whether you clicked to the left or right of your character, we can just compare the x-position of the character and that of cursorPosInWorld
, both of which are Vector3’s. Put it all together and you might have something like this:
Transform mainCharacter; //Set this to your character object's transform in the inspector
...
void Update()
{
...
if (Input.GetMouseButtonDown(0)) //Check for left click
{
cursorPosInWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if(cursorPosInWorld.x > mainCharacter.position.x)
{
//Move character right
}
else
{
//Move character left
}
}
...
}
Technically, the above will move the character left even if the cursor’s x-position is exactly equal to the character’s, which is vastly unlikely, but it will still move left/right based on click position.
I’m not sure what method you’re using to move your character though. If you need help with that part, too, let us know.
Let me know if you need a better explanation for any of this.
thanks Hyblademin, this is really helpful. The method that I am thinking of using to move the character is by screen touch to move for mobiles. I’m not sure if they’re the same as clicking as I hope they are ><. So, every time i tap on the right side of the screen, the player swim to the right once. So you have to constantly tapping for the player to keep swimming.
i got most part of it work.
I’m stuck with
if(cursorPosInWorld.x>mainCharacter,position.x)
{
//move player right. I do ( transform.position = new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f); )
}
but that teleport my player to the cursorPosInWorld position though ><. how do i move the player —> way if smoothly if i click right screen space.
Lerps should do the job