Simple 2D CLICK to MOVE Unity Tutorial

In this Unity Tutorial learn how to easily move GameObjects by clicking your mouse and having the objects head towards the position you clicked. This type of movement is typically found in Real Time Streategy or RTS games, but can be utilized for any top down game. Also this tutorial is made for 2D games, but the exact same script can apply to 3D games as well.
This does NOT include any talk of AI pathfinding, such as the A* algorithm to walk around walls/obstacles.
✨Want to support the channel?
Buy me a coffee ☕: ko-fi.com/bmoli
➤LIKE the video, it makes me big happy
➤SUBSCRIBE to make us both bigger happy.
➤Join our DISCORD SERVER: / discord
We have channels to help you with your struggles!
Thanks for watching!
#bmo #unity #tutorial

Пікірлер: 59

  • @m1kegame_dev456
    @m1kegame_dev456 Жыл бұрын

    Keep it up man, You just made a tutorial that was not only able to be followed but was easy to. Thanks!

  • @bornaniksic4795
    @bornaniksic4795 Жыл бұрын

    Thank you so much, you explained it very well.

  • @T3QN1Q
    @T3QN1Q2 жыл бұрын

    With this great power I have learned from you comes great responsibility. I will go forth and use this tutorial for both 2D and 3D games!

  • @BMoDev

    @BMoDev

    2 жыл бұрын

    do it, you won't

  • @SonmokPas56
    @SonmokPas5611 ай бұрын

    The video was helpful thanks!

  • @Stompin40
    @Stompin402 жыл бұрын

    Oh nice, going to show my class this video.

  • @flintdadson8662
    @flintdadson86622 жыл бұрын

    Thank you, your videos are great to learn

  • @BMoDev

    @BMoDev

    2 жыл бұрын

    Appreciate that!

  • @Gupatik
    @Gupatik2 жыл бұрын

    Bro plz keep it up. I see you one day with 300K subs

  • @mactram7293
    @mactram72934 ай бұрын

    tk for ur video, i'm learning unity

  • @BMoDev

    @BMoDev

    4 ай бұрын

    You got this!

  • @badsnipez
    @badsnipez8 ай бұрын

    FOR THOSE WHO ARE JUST GOING TO ONE SIDE OF SCREEN!!!: The fix is simply your Main Camera is very tiny and is in the one side of your scene. If you have a background that is 1080p, change your Main Cameras "Transform" attributes to the following: (just is the halves of the resolution 1920 x 1080, can do the math for other resolutions) X = 960 Y = 540 Under "Camera", change the size of the camera to be half as well (in my case is 540). This will make it so your character goes in the direction you want but very slowly. Instead of changing the code speed variable, go to where the script is attached to your player and change the speed from 5 to something like 100 or so. Hope this helps all of you, God knows it took me a couple hours to figure out on my own.

  • @thenicocarmo

    @thenicocarmo

    5 ай бұрын

    When i go to where you say under "camera" there's not a single thing called size or where i can change its size to 540, can you help me out figure what or where is that box to change the number to 540?

  • @badsnipez

    @badsnipez

    5 ай бұрын

    @@thenicocarmo I believe I just meant Main Camera size. The beginning part was just the position of where the camera should be to be in the middle of the screen (960x, 540y). Changing the size of it should make it expand and fit to all the edges and corners. I've been trying to load my unity project that I did it on but its being slow. Let me know if that helps.

  • @badsnipez

    @badsnipez

    5 ай бұрын

    @@thenicocarmo Just confirmed it is under the Main Camera game object for me. it is below Transform for me where you do the other things. Hope that helps.

  • @alichamas63
    @alichamas639 ай бұрын

    Thanks! Wondering why you don't just use the OnMouseDown method to set the target (ie. keep all the interactive stuff in there) and keep the Update method just about setting the position vector?

  • @Murlock135
    @Murlock135 Жыл бұрын

    I used it in a 3d game but it keeps going to the camera and not where I click

  • @mandamoon9149
    @mandamoon91492 жыл бұрын

    Bravo sir! Now I just need to use my boxes to breakdown that wall between us ;)

  • @BMoDev

    @BMoDev

    2 жыл бұрын

    I dont think so

  • @thedude4795
    @thedude479511 ай бұрын

    yeah i compiled the text with no errors but on reddit they say that OnMouseDown() is deprecated(?) and that causes the issue: BoxCollider does not support negative scale or size. im using a 3d scene with a sphere as Player, tried different collider geo's box, 2d, sphere etc, but i think theres something wrong

  • @3tr2band
    @3tr2band Жыл бұрын

    Hi, my problem is OnMouseDown not working. Can u help me?

  • @jaxonwooton8540
    @jaxonwooton8540 Жыл бұрын

    Could you show how to do the 2D pathfinder?

  • @brandon-tx4jo
    @brandon-tx4jo Жыл бұрын

    how can i make it rotate and flip base on direction it moves?

  • @EnemyOTS
    @EnemyOTS2 ай бұрын

    is there a way to flip character face when you click left and right ? i cant find it anywhere

  • @fajriturangga4818
    @fajriturangga48182 жыл бұрын

    how about the player is in canvas. i tried but it doesnt works well

  • @huimincoreydu1388
    @huimincoreydu13887 ай бұрын

    slim and clear

  • @dumby9067
    @dumby9067 Жыл бұрын

    i did this and it told me to add a } at the end of the first line of code so i did but then it told me that the } cant be there so its not working, does anyone have a fix for this?

  • @Zondie
    @Zondie6 ай бұрын

    i can select yes but i can not select the other and when i restart the game and click the other than the other one gets selectet again like wtf?

  • @eileeng2492
    @eileeng24922 жыл бұрын

    Wow first to comment. Love all these bids,keep them coming

  • @BMoDev

    @BMoDev

    2 жыл бұрын

    thanks!

  • @eggseatpegs
    @eggseatpegs2 жыл бұрын

    this dude deserves more subs Edit: wow i didnt expect he'd see my comment and even comment on it

  • @BMoDev

    @BMoDev

    2 жыл бұрын

    Thanks 😭

  • @deadbroadcastpc
    @deadbroadcastpc2 жыл бұрын

    🔥

  • @BMoDev

    @BMoDev

    2 жыл бұрын

    🔥🔥

  • @iicookfrogs4538
    @iicookfrogs4538 Жыл бұрын

    How would i make it rotate to face where i clicked?

  • @rezahageby4411

    @rezahageby4411

    11 ай бұрын

    Once you get the transform of the mouse position, use the: transform.LookAt(target); Put this in your object and it will rotate to face it. If you just want it to turn However Youll have to lock X and Z Rotations or else it might start doing flips once you go above or below it.

  • @starplatinum3305

    @starplatinum3305

    9 ай бұрын

    Vector (direction) = mouse position - object position Transform.up = direction

  • @PSEVDONIM228
    @PSEVDONIM228 Жыл бұрын

    i have multiple squares with different collors that i selected by myself in the inspector so how can i return the color of an object after i selected an another object?

  • @PSEVDONIM228

    @PSEVDONIM228

    Жыл бұрын

    because when i chose an another square it changes the color from my color to white

  • @mikey_r3
    @mikey_r3 Жыл бұрын

    I have UI buttons (D-pad) and a 2D player How do I make the player move automatically on the direction pressed (up/down/left/right)?? When you press a button once, player moves in that direction without stopping, and can change direction if another button is pressed Like Snake 2D

  • @Kooczsi

    @Kooczsi

    Жыл бұрын

    addforce

  • @riyamishal8890
    @riyamishal8890 Жыл бұрын

    Unity's wat version is this?

  • @TheMrDANIELLM
    @TheMrDANIELLM Жыл бұрын

    Don't Work in my project: Assets\Movement_Player.cs(7,24): error CS0246: The type or namespace name 'MovetoMouse' could not be found (are you missing a using directive or an assembly reference?) :/ Editor version: 2021.3.5f1

  • @TheMrDANIELLM

    @TheMrDANIELLM

    Жыл бұрын

    My code: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Movement_Player : MonoBehaviour { public static List moveableObjects = new List(); public float speed = 5f; private Vector3 target; private bool selected; void Start() { moveableObjects.add(this); target = transform.position; } // Update is called once per frame void Update() { if(Input.GetMouseButtonDown(1) && selected) { target = Camera.main.ScreenToWorldPoint(Input.mousePosition); target.z = transform.position.z; } transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime); } private void OnMouseDown() { selected = true; gameObject.GetComponent().color = Color.green; foreach (MovetoMouse obj in moveableObjects) { if(obj != this) { obj.selected = false; obj.gameObject.GetComponent().color = Color.white; } } } }

  • @hearoth00

    @hearoth00

    Жыл бұрын

    @@TheMrDANIELLM It's been 3 months but you should rename MovetoMouse to Movement_Player as that's the name of your script

  • @atharvkokate6882
    @atharvkokate6882Ай бұрын

    If anyone can't click / select their game object and nothing is moving, don't forget to add a collider2D to your object !

  • @lampooot
    @lampooot10 ай бұрын

    my game object is dynamic rigidbody its vibrating

  • @Markleap
    @Markleap Жыл бұрын

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class MoveToMouse : MonoBehaviour { public static List moveableObjects = new List(); public float speed = 5f; private Vector3 target; private bool selected; void Start() { moveableObjects.Add(this); target = transform.position; } // Update is called once per frame void Update() { if(Input.GetMouseButtonDown(0) && selected) { target = Camera.main.ScreenToWorldPoint(Input.mousePosition); target.z = transform.position.z; } transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime); } private void OnMouseDown() { selected = true; gameObject.GetComponent().color = Color.green; foreach(MoveToMouse obj in moveableObjects) { if(obj != this) { obj.enabled = false; obj.gameObject.GetComponent().color = Color.white; } } } }

  • @fextjd7564

    @fextjd7564

    7 ай бұрын

    thank you so much!

  • @Kasuga-
    @Kasuga-2 жыл бұрын

    can you make a tutorial on how to revive a character using ads? when he's dead for ex :- *player is dead* Restart Main Menu Watch an ad to Revive(and continues the game where it was left)

  • @mr.pillow

    @mr.pillow

    2 жыл бұрын

    Bruh

  • @BMoDev

    @BMoDev

    2 жыл бұрын

    bruh

  • @BMoDev

    @BMoDev

    2 жыл бұрын

    probably at some point

  • @Kasuga-

    @Kasuga-

    2 жыл бұрын

    @@BMoDev what about today? (or u ca'nt do that😏)

  • @pyguy4594
    @pyguy45942 жыл бұрын

    my square just goes to the bottom left

  • @kevinsihaloho4431
    @kevinsihaloho44312 жыл бұрын

    After my two boxes 'box'-boxing each other and then I come back to this video, I only found grey "subscribed" button but not the red "subscribe" button.. Sorry I can't make you happy :'( All I can do is vote up the video :"

  • @BMoDev

    @BMoDev

    2 жыл бұрын

    thank you for your valuable contribution - while pressing the red button brings me immense joy, the like button also brings me joy

  • @eileeng2492
    @eileeng24922 жыл бұрын

    Vids

  • @schweinsbradl7165
    @schweinsbradl7165 Жыл бұрын

    sry. doesnt work. player is stupid floating around. :( btw. very fast video imo