Child object not Following Parent :(

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:

8121254--1052582--upload_2022-5-11_22-23-1.png

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 :confused:

Thanks guys!!

You don’t need to access an object’s Rigidbody to get its rotation, those are values you can already get by just accessing its transform.

Hey!! I had that idea, i tried to do something like this:

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

public class weaponScript : MonoBehaviour
{

    //public Rigidbody2D rb;
    public GameObject gameObject;
    private Vector2 rb;
    public Camera cam;
    Vector2 mousePos; 
 
    void Update()
    {
        mousePos = cam.ScreenToWorldPoint(Input.mousePosition);   

    }

    void FixedUpdate()
    {
        
        rb= gameObject.transform.position;    
        Vector2 aimDirection = mousePos - rb ;
        float angle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg;      
        gameObject.transform.rotation = Quaternion.Euler(0f, 0f, angle);



    }
}

Googled a little bit more, struggled with the Euler bit but all manageable :slight_smile:

Now it works, small bug with the sprite but i think i can make it work eventually :slight_smile:

Thank you very much :smile: