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