# Movement similar to Diablo, Rotation problems

Hello,

I`ve been trying to make a character movement similar to Diablo, but the player will move based on the keyboard (AWSD) and the torso will look at where the mouse is pointing.

It is in fact very similar to the Angry Bots demo from Unity. However, I`ve looked extensively at the scripts in Angry Bots and unfortunately the one responsible for the rotations is… beyond my capabilities =/

So I`ve managed to do it in another way, quite simple, but with one problem. Considering the player moves in direction Dir, and looks in direction Look, I check if (Vector3.Dot(Dir, Look) < 0) and then I know the character is looking backwards. If that is the case, I rotate the character 180 degrees and play the backwards animation instead, so the torso is not twisted in awkward directions. The problem is, since it is a 180 degree rotation, sometimes, the mouse rotates in one direction, and the character legs in another.

Look at this image:

Is there a smarter way to do this? I`ve been trying to solve this for days now =/

Thank you for your time and attention,
Best regards,
Allan

Well, assuming your character is in the center of the screen… You can set it up so that the character looks relative to the mouse’s position by basically drawing a vector from the center to the mouse and then making that relative to the camera.

The center of the screen is Screen.width * .5, Screen.height * .5, so the vector would be…

(Input.mousePosition - Vector3(Screen.width * .5, Screen.height * .5, 0)).normalized;

Given the above, the player would have to:

``````var camForward : Vector3 = Vector3(Camera.main.transform.forward.x, 0, Camera.main.transform.forward.z);
var camRight : Vector3 = Vector3(Camera.main.transform.right.x, 0, Camera.main.transform.right.z);
var lookVector : Vector3 = (Input.mousePosition - Vector3(Screen.width * .5, Screen.height * .5, 0)).normalized;

transform.LookAt(transform.position + lookVector.x * camRight + lookVector.y * camForward);
``````

Something like that. Completely untested, may have to invert the Input.mousePosition.y, but should always have the character looking at the mouse.

Once the character is looking at the mouse, you can do a simple comparison of movement vector to transform.forward to determine animations.

Actually, this is very simple. Assume you have a generic mover from here:

You want to first, create a plane at the point of the character, then get where the mouse is relation to it by casting a ray onto it and finding it’s intersection point. Simply then, look at that point, and it’s golden.

``````var plane : Plane = new Plane(Vector3.up, transform.position);
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var dist : float;
if(plane.Raycast(ray, dist)){
transform.LookAt(ray.GetPoint(dist));
}
``````

The supplied code simply tells it which direction it needs to go in. You could easily then control then character’s look with the mouse and the movement with moveDirection.

Only downside of that technique is that you need an additional object and a raycast, where the above technique only needs a little math…

That won’t really matter to the game’s speed, of course, but hey.

Its been a long time…

In the end, we changed a lot of this project and this was no longer an issue for me, but I never got this to work 100%.

I aws actually doing something very similar to bigmisterb`s suggestion, controlling the character with the keyboard and rotation through the mouse… the problem is the animations. In that case, I had a forward running animation, and reverse running animation, so I would check:

If Player is moving to direction (left), and looking to direction (left + x Angles), use forward running animation, if looking in opposite direction (about 90+ degrees from movement direction), play reverse walking animation. So basically if the player is moving and looking forward, thats fine, if he is looking backwards, thats also fine. The problem is the exact point where the animation changes from forward to reverse. The legs (sometimes) spin in the wrong direction. Like on the image on the first post.

The problem is only the look, the mechanic was working fine… but the animation… ruined it =/