I am currently developing a 2D Dungeon Crawler game. If you take a look at the screenshots, the aiming mode I’m trying to create is a detached arm from the body that will follow your cursor wherever it is. It is currently working for the most part, but it stops working whenever the player is moving. I cannot aim the arm whenever I am in the middle of moving the Player, but when I stop aiming I can aim the arm. Any solutions?
Here is a look at my Player code and Arm code just in case:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
Vector2 movement;
// Update is called once per frame
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArmMovement : MonoBehaviour
{
public Rigidbody2D armrb;
public Camera cam;
Vector2 mousePos;
// Update is called once per frame
void Update()
{
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
void FixedUpdate()
{
Vector2 lookDir = mousePos - armrb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 10f;
armrb.rotation = angle;
}
}