2.5D TopDown Sword/bow attack with mouse and character rotating towards mouse

Hi! so i’ve implemented a simple script in my game that allows for a cube to move with WASD and rotate towards the mouse cursor (and attack) Problem is, the cube is 2D so really all i did was make the cube rotate towards the mouse clockwise/counterclockwise, but as soon as i actually use a 2.5D sprite now that sprite just rotates on the wrong axis (rotates clockwise) and not around it self in “3d” space. I’m not sure how i could implement this and i can’t seem to find any help online.

Example
gifernnn

My PlayerController script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl : Character
{

    [SerializeField]
    private HealthStat health;


    public float moveSpeed = 5f;
    public Rigidbody2D rb;
    public Weapon weapon;
    private float initHealth = 100;

    private float activeMoveSpeed;
    public float dashSpeed;

    public float dashLength = .5f, dashCooldown = 2f;

    private float dashCounter;
    private float dashCoolCounter;

    Vector2 moveDirection;
    Vector2 mousePosition;



    protected override void Start()
    {
        activeMoveSpeed = moveSpeed;
        health.Initialize(initHealth, initHealth);

        base.Start();
    }
    // Start is called before the first frame update


    // Update is called once per frame
    protected override void Update()
    {
        GetInput();
        float moveX = Input.GetAxisRaw("Horizontal");
        float moveY = Input.GetAxisRaw("Vertical");

        if (Input.GetMouseButtonDown(0))
        {
            weapon.Fire();
        }

        moveDirection = new Vector2(moveX, moveY).normalized;
        mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        if (Input.GetKeyDown(KeyCode.LeftShift))
        {

            if (dashCoolCounter <= 0 && dashCounter <= 0)
            {
                activeMoveSpeed = dashSpeed;
                dashCounter = dashLength;
            }
        }
        if (dashCounter > 0)
        {
            dashCounter -= Time.deltaTime;

            if (dashCounter <= 0)
            {
                activeMoveSpeed = moveSpeed;
                dashCoolCounter = dashCooldown;
            }
        }


        if (dashCoolCounter > 0)
        {
            dashCoolCounter -= Time.deltaTime;
        }



        base.Update();
    }



    private void GetInput()
    {



        //debugging
        if (Input.GetKeyDown(KeyCode.I))
        {
            health.MyCurrentValue -= 10;
        }
        if (Input.GetKeyDown(KeyCode.O))
        {
            health.MyCurrentValue += 10;
        }




    }

    private void FixedUpdate()
    {
        rb.velocity = new Vector2(moveDirection.x * activeMoveSpeed, moveDirection.y * activeMoveSpeed);

        Vector2 aimDirection = mousePosition - rb.position;
        float aimAngle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg - 90f;
        rb.rotation = aimAngle;

        
    }
}

This is purely a passion project for me and my little brother so help is very much appreciated.

1 Answer

1

Are you using a 3D object but still have a Rigidbody2D? RigidBody2D only understands rotation around the Z axis, so if want to rotate your character around a different axis, then you can’t use rb.rotation.

You might want to take a look at Quaternion LookRotation. This lets you rotate around an axis that you define.

For example, I think this code will let you rotate around the Y axis. I’ve only ever used this in a pure-2D use case, so it may require some tweaking.

Quaternion rotation = Quaternion.LookRotation(new Vector3(0, 1, 0), aimDirection);
transform.rotation = rotation;