Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question throwing code doesnt work help pls

Discussion in 'Scripting' started by shiko_, Dec 1, 2023.

  1. shiko_


    Nov 19, 2023
    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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor.Callbacks;
    4. using UnityEngine;
    6. public class needleAttack : MonoBehaviour
    7. {  public float Speed = 4;
    10. public Transform Player;
    12.     void Start()
    13.     {
    16.      gameObject.SetActive(false);
    17.     }
    19.     void Update()
    20.     {
    21.       if (Input.GetKey("k")){
    22.          gameObject.SetActive(false);
    23.          if (Input.GetKey("e"))
    24.          {  transform.DetachChildren();
    25.             Rigidbody rb = GetComponent<Rigidbody>();
    26.           rb.AddForce (transform.right * Speed);
    27.     }
    28.       }
    34.       }
    35.      }

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

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


    Feb 8, 2018
    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
    Kurt-Dekker likes this.
  3. Kurt-Dekker


    Mar 16, 2013
    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: