2D Two Layer Player Controller

Hello,

I’m trying to make a 2D top down shooter. Hopefully in the future I hope to expand this a little more to incorporate some basic construction, but I digress. My focus right now is on the controller.

My idea was to have a two layer sprite, bottom layer being the legs, top layer being the torso and the arms. The top layer would have a controller script to follow the direction of the mouse. I have that here:

void FixedUpdate()
{
    Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    Quaternion rot = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward);
    transform.rotation = rot;
    transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);
}

Which that does a great job rotating the character wherever the mouse is.

What I’m working on now is the direction of the legs. I have several animations to include strafing and back stepping, the range I’m basing on will be:

“W” - Moving Forward
“W+A” / “W+D” - Moving at a 45 degree angle
“A” / “D” - Strafing left or right
“S” - Back step

Instead of W being up on the screen, WASD should be based on where the mouse is.

I’ve looked around to several places and things don’t really seem to be doing what I think they should:

void FixedUpdate()
{

    float inputF = Input.GetAxis("Vertical");
    float inputR = Input.GetAxis("Rotate");


    Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

    if (inputR == 0)
        transform.rotation = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward);
    //anim.SetBool("Moving", true);
    if (inputR > 0)
    {
        Vector3 rV = Quaternion.Euler(45,0,0) * mousePosition;
        transform.rotation = Quaternion.LookRotation(transform.position + rV, Vector3.forward);
    }
    if (inputR < 0)
    {
        Vector3 rV = Quaternion.Euler(45,0,0) * mousePosition;
        transform.rotation = Quaternion.LookRotation(transform.position - rV, Vector3.forward);
    }

    Debug.Log("MouseX = " + mousePosition.x);

    transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);
}

I’m pretty sure I’m over thinking this, and this isn’t the only method I’ve tried. They all seem to yield the same results though, the feet will rotate with the mouse if nothing is pressed, but if you press something, the feet move one direction or the other just slightly, not 45 degrees.

EDIT

After doing a little more research, I found an approach that seems to be more on par with what I’m trying to achieve… I’m also trying to actually rotate about the right axis.

What I’m seeing though is the rotation seems to only be on one half. IE, if my mouse is north of the sprite, the feet rotate based on the mouse’s location (granted not exactly the right way, but we’re still taking baby steps). If I move the mouse below, the feet still bounce back and forth facing the top as if the mouse were still above the sprite.

So… Got it.
Not a complete controller, but the framework is there at least. Turned out a lot more simple than I definitely thought originally. If anyone else can benefit, here’s my new code:

using UnityEngine;
using System.Collections;

public class PlayerFeetController : MonoBehaviour
{

Animator anim;
private float rotateOffset = 0f;

void Start()
{
    //anim = GetComponent<Animator>();
}

void FixedUpdate()
{

    float inputF = Input.GetAxis("Vertical");
    float inputR = Input.GetAxis("Rotate");

    Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

    //Standing Still
    if (inputR == 0 && inputF == 0)
        rotateOffset = 0;

    //Walking Forward
    if (inputR == 0 && inputF > 0)
    {
        rotateOffset = 0;
    }

    //Walking Angle Left
    if (inputR > 0 && inputF > 0)
    {
        rotateOffset = -45;
    }

    //Walking Angle Right
    if (inputR < 0 && inputF > 0)
    {
        rotateOffset = 45;
    }

    //Walking Left
    if (inputR > 0 && inputF == 0)
    {
        rotateOffset = 0;
        //set Animation
    }

    //Walking Right
    if (inputR < 0 && inputF == 0)
    {
        rotateOffset = 0;
        //set Animation
    }

    //Walking Backwards
    if (inputF < 0)
    {
        //set Animation
    }

    transform.rotation = Quaternion.AngleAxis(rotateOffset, Vector3.forward) * Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward);
    transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);
}
}

So step one complete, I’m going to work on my animation controller now. After that is the procedurally ‘infinite’ tile terrain which I’m sure I’ll have another post with a question or two on.