Simple 2D CLICK to MOVE Unity Tutorial

Поділитися
Вставка
  • Опубліковано 25 лис 2024

КОМЕНТАРІ •

  • @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 10 місяців тому

      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 10 місяців тому

      @@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 10 місяців тому

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

  • @T3QN1Q
    @T3QN1Q 3 роки тому +7

    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

  • @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!

  • @3tr2band
    @3tr2band 2 роки тому +5

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

  • @Stompin40
    @Stompin40 3 роки тому +1

    Oh nice, going to show my class this video.

  • @mactram7293
    @mactram7293 10 місяців тому

    tk for ur video, i'm learning unity

    • @BMoDev
      @BMoDev  10 місяців тому

      You got this!

  • @SonmokPas56
    @SonmokPas56 Рік тому +2

    The video was helpful thanks!

  • @Gupatik
    @Gupatik 2 роки тому +1

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

  • @bornaniksic4795
    @bornaniksic4795 2 роки тому +1

    Thank you so much, you explained it very well.

  • @Murlock135
    @Murlock135 Рік тому +8

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

    • @Xurium
      @Xurium 2 місяці тому

      Yeah same, did you find a solution?

  • @flintdadson8662
    @flintdadson8662 3 роки тому +2

    Thank you, your videos are great to learn

    • @BMoDev
      @BMoDev  2 роки тому

      Appreciate that!

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

  • @eggseatpegs
    @eggseatpegs 2 роки тому +3

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

    • @BMoDev
      @BMoDev  2 роки тому +1

      Thanks 😭

  • @jaxonwooton8540
    @jaxonwooton8540 2 роки тому +2

    Could you show how to do the 2D pathfinder?

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

  • @brandon-tx4jo
    @brandon-tx4jo 2 роки тому +2

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

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

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

  • @huimincoreydu1388
    @huimincoreydu1388 Рік тому

    slim and clear

  • @fajriturangga4818
    @fajriturangga4818 2 роки тому +1

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

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

  • @eileeng2492
    @eileeng2492 3 роки тому +1

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

  • @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?

  • @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?

  • @EnemyOTS
    @EnemyOTS 7 місяців тому

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

  • @atharvkokate6882
    @atharvkokate6882 6 місяців тому

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

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

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

  • @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😏)

  • @deadbroadcastpc
    @deadbroadcastpc 3 роки тому +2

    🔥

  • @lampooot
    @lampooot Рік тому

    my game object is dynamic rigidbody its vibrating

  • @pyguy4594
    @pyguy4594 2 роки тому

    my square just goes to the bottom left

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

  • @schweinsbradl7165
    @schweinsbradl7165 2 роки тому +2

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

  • @arturogonzalezchavez6471
    @arturogonzalezchavez6471 Місяць тому

    Gane

  • @eileeng2492
    @eileeng2492 3 роки тому

    Vids