1
\$\begingroup\$

I am trying to make a 2D RPG game in Unity with an isometric tile setup.

Using Unity/C#, how do I code the character's upward, right, left, down and diagonal animations and movements using mouse button as the controls (much like Red Alert 2). Normally, this can easily be done using keyboard inputs, however, what I am trying to do is control the character by clicking the mouse. For example, if I click in a diagonal location from the character, the character should follow the mouse and also do the diagonal animation.

\$\endgroup\$
1
\$\begingroup\$

The way I would approach this is casting a ray from the camera in the direction the mouse has clicked in order to find the position on the map. This should give you the position on the terrain as a Vector3. Then use basic vector maths to find the direction in which your character should move:

Vector3 direction = character.position - target.position
direction.Normalize()

You can then calculate a velocity for the character using the direction:

Vector3 velocity = direction * speed; // ex: float speed = 5;

Then use this velocity accordingly:

character.position += velocity * Time.deltaTime;

This is the way I would implement it anyway. In order for the character to move around obstacles, you need to research pathfinding.

The direction the character is moving to is the direction the character model should face as well.


How to raycast from the camera: https://docs.unity3d.com/Manual/CameraRays.html

Direction from one vector to another: https://docs.unity3d.com/Manual/DirectionDistanceFromOneObjectToAnother.html

Basic top-down movement: https://unity3d.com/learn/tutorials/topics/2d-game-creation/top-down-2d-game-basics

\$\endgroup\$
0
\$\begingroup\$

This tutorial off the Unity website should help you with the entire project, especially if you're trying to emulate the Red Alert series.

Player movement and animation using the mouse is covered in Chapter 2, right after they set up the environment, if you want to just skip to that. The tutorial's written in Unity 4.6, but they have the 5.x project files on there as well so it shouldn't be all too hard to make the switch.

Better yet, it's beginner-level and includes both the code and a video, so it'll be relatively easy to pull off.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .