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
Keep it up man, You just made a tutorial that was not only able to be followed but was easy to. Thanks!
Thank you so much, you explained it very well.
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
2 жыл бұрын
do it, you won't
The video was helpful thanks!
Oh nice, going to show my class this video.
Thank you, your videos are great to learn
@BMoDev
2 жыл бұрын
Appreciate that!
Bro plz keep it up. I see you one day with 300K subs
tk for ur video, i'm learning unity
@BMoDev
4 ай бұрын
You got this!
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
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
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
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.
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?
I used it in a 3d game but it keeps going to the camera and not where I click
Bravo sir! Now I just need to use my boxes to breakdown that wall between us ;)
@BMoDev
2 жыл бұрын
I dont think so
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
Hi, my problem is OnMouseDown not working. Can u help me?
Could you show how to do the 2D pathfinder?
how can i make it rotate and flip base on direction it moves?
is there a way to flip character face when you click left and right ? i cant find it anywhere
how about the player is in canvas. i tried but it doesnt works well
slim and clear
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?
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?
Wow first to comment. Love all these bids,keep them coming
@BMoDev
2 жыл бұрын
thanks!
this dude deserves more subs Edit: wow i didnt expect he'd see my comment and even comment on it
@BMoDev
2 жыл бұрын
Thanks 😭
🔥
@BMoDev
2 жыл бұрын
🔥🔥
How would i make it rotate to face where i clicked?
@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
9 ай бұрын
Vector (direction) = mouse position - object position Transform.up = direction
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
Жыл бұрын
because when i chose an another square it changes the color from my color to white
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
Жыл бұрын
addforce
Unity's wat version is this?
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
Жыл бұрын
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
Жыл бұрын
@@TheMrDANIELLM It's been 3 months but you should rename MovetoMouse to Movement_Player as that's the name of your script
If anyone can't click / select their game object and nothing is moving, don't forget to add a collider2D to your object !
my game object is dynamic rigidbody its vibrating
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
7 ай бұрын
thank you so much!
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
2 жыл бұрын
Bruh
@BMoDev
2 жыл бұрын
bruh
@BMoDev
2 жыл бұрын
probably at some point
@Kasuga-
2 жыл бұрын
@@BMoDev what about today? (or u ca'nt do that😏)
my square just goes to the bottom left
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
2 жыл бұрын
thank you for your valuable contribution - while pressing the red button brings me immense joy, the like button also brings me joy
Vids
sry. doesnt work. player is stupid floating around. :( btw. very fast video imo