Unity RTS Movement Tutorial - How to Move Characters in Unity by Clicking | MOBA Control Tutorial

preview_player
Показать описание

Hey Game Developers!

Bilal here, and welcome to another Unity Tutorial!

In this one, we’ll create a MOBA character controller or RTS character controller so that our player can move to a clicked destination. This technique also works very well for diablo controls or dungeon crawler controls.

See you in the next tutorial,
Bilal

If you want to get awesome benefits like personalized tutorials, support us on patreon!

MORE:
Рекомендации по теме
Комментарии
Автор

Great Tutorial! Any hint on how to expand your code to a box selection with a list of selected units and a dynamic group movement into the clicked position?

guilhermecastellani
Автор

4:47 any specific reason why you used ScreenToWorldPoint instead of ScreenPointToRay?
I would think that it'd be nice to get the finished Ray from the builtin function to save a few lines of code, and it would work better with a perspective projection camera.

JonWaterfall
Автор

This is how it should look for a perspective camera, I had to look it up, but the other way should work if you have the camera at the correct distance. the issue is that you trace a ray from the perspective camera and along the cameras Z axis and not along the field of view.

private Vector3 GetWorldPosisionFromMouse()
{
/*
Vector2 ScreenPosition = Input.mousePosition;
Vector3 MouseWorldPosision =



Physics.Raycast(MouseWorldPosision, cam.transform.forward, out HitPosition, 1000f, GroundLayer);
*/
RaycastHit HitPosition;

Ray ray =

Physics.Raycast(ray, out HitPosition);

return HitPosition.point;
}

alfonsmarklen
Автор

This is a great tutorial! I have one issue so far and it feels like my setup more so than what you had here. My unit(player) moves to the center of the screen rather then the area of the right click. Still though, a solid tutorial. Keep up the good work.

AzirinSol
Автор

Please make a tutorial to display a target cursor in the ground when set position

cfactorygames
Автор

I keep getting "Object reference not set to an instance of an object". I have the ground set as ground "layer" and I have it set up in the script as well. Not sure what could be wrong.

TheNesdsachannel
Автор

Ok, great tutorial but how do I make this work for a perspective camera and not just a orthographic one. It took a few moments to realize the bug.

alfonsmarklen
Автор

anyone know why my character might be moving towards the center of the camera viewpoint every time?

Benjackalope
Автор

How to select multiple troops with shift click or Box selection? and unselect troops

martin-gaming
Автор

Would "onPointerClick" handler work better than raycast?

lehula
Автор

Hi great tutorial but I am having a problem the raycast is giving me position of the centre of the camera not the mouse and checked your script and mine and it looks same double checked everything and still having the problem

aexere
Автор

Hello, thank you for the time you spent doing this.

Can you please help me out and figure how can I adapt your script into my game? I tried and followed everything you teach, but maybe I am doing something wrong:

My project was created from Unity's Roll a ball tutorial and in the meanwhile I'm trying to implement diferent features e.g: timer, scores, UI and movement.
That so, from your explanation and my project, the only difference I am seeing is that my level is a plane (I can't figure out how you made your level, but also I don't know if that matters)
This is my code:

using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{
Camera cam;

public LayerMask groundLayer;

public NavMeshAgent playerAgent;

public float speed;
public Text countText;
public Text winText;

private int count;

void Awake()

{
cam = Camera.main;
count = 0;
SetCountText();
winText.text = "";
}

void Update()
{
if (Input.GetMouseButtonDown(1))
{

}
}

private Vector3 GetPointUnderCursor()
{
Vector2 screenPosition = Input.mousePosition;
Vector3 mouseWorldPosition =

RaycastHit hitPosition;

Physics.Raycast(mouseWorldPosition, cam.transform.forward, out hitPosition, 100, groundLayer);

return hitPosition.point;
}

void OnTriggerEnter(Collider other)
{
if Up"))
{

count = count + 1;
SetCountText();
}
}

void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 14)
{

winText.text = "You Win!";
}
}
}

I've made the Navigation Part and the Layer Part also and assigned both in my Player - with the PlayerController.cs
Somehow the movement still doens't work.

Glad if you could help!
Cheers!

unskyy
Автор

I'm also having some troubles, I'll see what I can do...

lochrowley
Автор

You could explain to us how to shoot by clicking (Attacking a Selected Enemy)

kingofgame
Автор

Not sure what i did wrong, the character only moves straight up in one direction, instead of where i clicked.

MacMashPotato
Автор

So I really appreciate this tutorial. But as others have suggested, it looks like the only way for this to work is to use an Orthographic camera instead of Perspective, otherwise your player will just head to the center of the map. Thing is, Orthographic gives me a headache to look at, and Perspective just looks much better. Does anyone know of another method that doesn't require the camera to be Orthographic? Thanks!

singleplayermoments
Автор

i love how every single one of these tutorials only show 1 unit moving. There is a reason why too. No one knows how to do this correctly. Try moving 200+ units without FPS loss and facing the correct direction.

Ziron
Автор

Was looking for a movement indicator, like you SHOWED IN YOUR THUMBNAIL...-Absolute waste of time. Please ensure that what you show Is equal to the presented thumbnail.

mikkelgedehansen
Автор

Still looking for RTS Movement....man you should get dislike for fake name of the video. I will be kind and will not give it.
Will be great if you can fix it and make video about RTS movement, where at list 3 characters will move together ;)

volodymyrsaruta