throwing code doesnt work help pls

hey guys, first time posting here, so sorry if i do something wrong, but heres the issue

i was trying to make a game where you shoot a needle, then it comes back
ive parented the needle to my characters hip, then it unparents, shoots forward and comes back
the thing is, i have yet to make the needle come back, and the needle doesnt move with the scripti have written
this is the code in question

using System.Collections;
using System.Collections.Generic;
using UnityEditor.Callbacks;
using UnityEngine;

public class needleAttack : MonoBehaviour
{  public float Speed = 4;

  
public Transform Player;

    void Start()
    {


     gameObject.SetActive(false); 
    }

    void Update()
    {
      if (Input.GetKey("k")){
         gameObject.SetActive(false);
         if (Input.GetKey("e"))
         {  transform.DetachChildren();
            Rigidbody rb = GetComponent<Rigidbody>();
          rb.AddForce (transform.right * Speed);
    }
      }
    

    

    
      }
     }

heres the scene if you need it btw
9505099--1339153--Captura de pantalla 2023-12-01 163153.png

also i used the 2d tag bc i didnt know what to put srry

So theres a bit to unpack here but ill start off simple. You are using Input.GetKey which gets called many many times per press. Throw in a Debug.Log and you’ll see in the Console window how many times it gets called per press or if you hold it.

You probably want GetKeyDown or GetKeyUp. Those only get called once per Down/Up. Now check that same Debug.Log and see how many times it gets called.

So what is happening with yours is that the gameObject.SetActive(false) is being called many many times when you probably only need to call it once. Then, you have to you are having to press E while K is pressed which will do all of that code many many times per press. So you are detatching the children many many times, getting a reference to the Rigidbody (not Rigidbody2D which is what you want since youre doing 2D) many many times per press (ONLY DO THIS ONCE, using GetComponent takes a lot of overhead resources) and then adding that force many many times (you may want that, but maybe not).

Get the Reference to the Rigidbody2D (make sure that is the component on your Player, not a Rigidbody) at Start or via a public field.

Again, theres a lot going on, but hopefully some of what i said tracks and gets you closer to your desired result

1 Like

There’s no movement code above to do anything like ‘shoots forward and comes back’…

You probably want to look into using a tweener such as DOTWeen or LeanTween to do the movement.

You also need to consider lifecycle such as how this needleAttack script will get turned back on again, since you disable it above. Disabling a script is certainly legal but slightly overkill when you can simply keep your own variables indicating that the needle attack is underway or if it has ended.

To get a handle on all this complexity, you will need to work one small step at a time, like this guy:

Imphenzia: How Did I Learn To Make Games: