Simple 2D CLICK to MOVE Unity Tutorial

Поделиться
HTML-код
  • Опубликовано: 20 дек 2024

Комментарии • 60

  • @m1kegame_dev456
    @m1kegame_dev456 2 года назад +5

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

  • @T3QN1Q
    @T3QN1Q 3 года назад +8

    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  3 года назад +2

      do it, you won't

  • @3tr2band
    @3tr2band 2 года назад +6

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

  • @badsnipez
    @badsnipez Год назад +3

    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 11 месяцев назад

      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 11 месяцев назад

      @@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 11 месяцев назад

      @@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.

  • @mactram7293
    @mactram7293 11 месяцев назад

    tk for ur video, i'm learning unity

    • @BMoDev
      @BMoDev  10 месяцев назад

      You got this!

  • @SonmokPas56
    @SonmokPas56 Год назад +2

    The video was helpful thanks!

  • @Murlock135
    @Murlock135 Год назад +8

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

    • @Xurium
      @Xurium 3 месяца назад

      Yeah same, did you find a solution?

  • @Gupatik
    @Gupatik 2 года назад +1

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

  • @flintdadson8662
    @flintdadson8662 3 года назад +2

    Thank you, your videos are great to learn

    • @BMoDev
      @BMoDev  3 года назад

      Appreciate that!

  • @TheMrDANIELLM
    @TheMrDANIELLM 2 года назад +2

    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 2 года назад

      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 2 года назад

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

  • @thedude4795
    @thedude4795 Год назад +1

    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

  • @bornaniksic4795
    @bornaniksic4795 2 года назад +1

    Thank you so much, you explained it very well.

  • @iicookfrogs4538
    @iicookfrogs4538 2 года назад +3

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

    • @rezahageby4411
      @rezahageby4411 Год назад +1

      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 Год назад

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

  • @brandon-tx4jo
    @brandon-tx4jo 2 года назад +2

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

  • @mandamoon9149
    @mandamoon9149 3 года назад +2

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

    • @BMoDev
      @BMoDev  3 года назад

      I dont think so

  • @fajriturangga4818
    @fajriturangga4818 2 года назад +1

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

  • @jaxonwooton8540
    @jaxonwooton8540 2 года назад +2

    Could you show how to do the 2D pathfinder?

  • @EnemyOTS
    @EnemyOTS 8 месяцев назад

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

  • @PSEVDONIM228
    @PSEVDONIM228 2 года назад +1

    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 2 года назад

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

  • @dumby9067
    @dumby9067 Год назад +1

    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?

  • @mikey_r3
    @mikey_r3 2 года назад +1

    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 2 года назад

      addforce

  • @alichamas63
    @alichamas63 Год назад

    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?

  • @riyamishal8890
    @riyamishal8890 2 года назад +1

    Unity's wat version is this?

  • @Markleap
    @Markleap Год назад +10

    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;
    }
    }
    }
    }

  • @Stompin40
    @Stompin40 3 года назад +1

    Oh nice, going to show my class this video.

  • @eggseatpegs
    @eggseatpegs 3 года назад +3

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

    • @BMoDev
      @BMoDev  3 года назад +1

      Thanks 😭

  • @eileeng2492
    @eileeng2492 3 года назад +1

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

  • @Kasuga-
    @Kasuga- 3 года назад +1

    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 3 года назад

      Bruh

    • @BMoDev
      @BMoDev  3 года назад +1

      bruh

    • @BMoDev
      @BMoDev  3 года назад

      probably at some point

    • @Kasuga-
      @Kasuga- 3 года назад

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

  • @huimincoreydu1388
    @huimincoreydu1388 Год назад

    slim and clear

  • @lampooot
    @lampooot Год назад

    my game object is dynamic rigidbody its vibrating

  • @atharvkokate6882
    @atharvkokate6882 7 месяцев назад

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

  • @deadbroadcastpc
    @deadbroadcastpc 3 года назад +2

    🔥

    • @BMoDev
      @BMoDev  3 года назад

      🔥🔥

  • @kevinsihaloho4431
    @kevinsihaloho4431 3 года назад +1

    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  3 года назад +2

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

  • @pyguy4594
    @pyguy4594 2 года назад

    my square just goes to the bottom left

  • @schweinsbradl7165
    @schweinsbradl7165 2 года назад +2

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

  • @arturogonzalezchavez6471
    @arturogonzalezchavez6471 2 месяца назад

    Gane

  • @eileeng2492
    @eileeng2492 3 года назад

    Vids