Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Firework Script Not Working!

Discussion in 'Scripting' started by JVGameDev, Jul 12, 2016.

  1. JVGameDev

    JVGameDev

    Joined:
    Oct 5, 2015
    Posts:
    137
    Hi! I am trying to make a firework go up if you are 3 feet away from it and you press X, but it doesn't work.

    Code (csharp):
    1.  
    2.   public Transform FireWork;
    3.   public Rigidbody rb;
    4.   public int Thrust;
    5.    // Use this for initialization
    6.    void Start () {
    7.    
    8.    }
    9.    
    10.    // Update is called once per frame
    11.    void Update () {
    12.   if (Input.GetKeyDown(KeyCode.X))
    13.   {
    14.   if (Vector3.Distance(transform.position, FireWork.position) < 3)
    15.   {
    16.   transform.Translate(Vector3.up * Time.deltaTime);
    17.   Debug.Log("You Can Light It!");
    18.    
    19.   }
    20.   }
    21.  
    22.   }
    23. }
    24.  
    the firework only jitters a tiny bit and then does nothing.
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    What I'm seeing here is that, while holding down the X key, and being within 3 units of the FireWork object, the object that has this script will translate upwards by a tiny amount each frame. However, I also see a reference to a rigidbody. That suggests that physics is enabled. If physics is enabled, then gravity is in play. Gravity will push down on the object every frame. So, even though holding down the X button will cause the object to go upwards by a little bit, the amount that you're pushing up may not be enough to overcome gravity. In fact, I'm pretty sure it isn't since you're only moving the object up by 1 * deltaTime, while gravity pushes down by -9*deltaTime.

    I'd suggest increasing the amount of upwards translation to see if it can overcome gravity.
     
  3. JVGameDev

    JVGameDev

    Joined:
    Oct 5, 2015
    Posts:
    137
    Ok. While i got you here, i was also wondering how do i set it after 2 seconds to explode?