Hey guys, sry for going full newbie on this, ive looked for several forums but i cant quite get the exact representation of my problem.
Im working on a 2D platformer with shooting, something like Neon Abyss type of game. im doing it from scratch for the knowledge mostly.
The thing is that i get to this point where i have an object player and i add an weapon object to it as a child so i can make it spin around following my cursor. The thing works until i move my player object i walk like 1 unit in the X and the weapon de-attaches and moves randomly.
This is the weapon in the inspector:
And the code in the WeaponScript is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class weaponScript : MonoBehaviour
{
public Rigidbody2D rb;
public Camera cam;
Vector2 mousePos;
void Update()
{
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
void FixedUpdate()
{
Vector2 aimDirection = mousePos - rb.position;
float angle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg;
rb.rotation = angle;
}
}
Correct me if im wrong, looking around in the forum i read that adding a RigidBody to the weapon might be the issue here, but i dont know any other way to get the transform of the weapon Object so i can spin it
Thanks guys!!