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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

[SOLVED] SpringJoint2D switching on after pressing the key

Discussion in 'Scripting' started by ProExCurator, May 4, 2020.

  1. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerPush : MonoBehaviour
    6. {
    7.  
    8.     private GameObject box;
    9.     private BoxPull boxpull;
    10.     private float distance;
    11.     private float minimumDistance = 2f;
    12.  
    13.     void Start()
    14.     {
    15.         box = GameObject.FindWithTag("Pushable");
    16.     }
    17.     void Update()
    18.     {
    19.         distance = Vector3.Distance(box.transform.position, transform.position);
    20.         if(distance <= minimumDistance)
    21.         {
    22.             if (Input.GetKeyDown(KeyCode.E))
    23.             {
    24.                 boxpull = box.GetComponent<BoxPull>();
    25.                 boxpull.jointspring = 1;
    26.             }
    27.         }
    28.         if (Input.GetKeyDown(KeyCode.E) && boxpull.jointspring == 1)
    29.         {
    30.             boxpull.jointspring = 0;
    31.         }
    32.     }
    33. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BoxPull : MonoBehaviour
    5. {
    6.     public int jointspring = 0;
    7.     public SpringJoint2D springjoint;
    8.  
    9.     void Start()
    10.     {
    11.         springjoint = gameObject.GetComponent<SpringJoint2D>();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void FixedUpdate()
    16.     {
    17.         if(jointspring == 1)
    18.         {
    19.             springjoint.enabled = true;
    20.         }
    21.         if(jointspring == 0)
    22.         {
    23.             springjoint.enabled = false;
    24.         }
    25.     }
    26. }
    The whole script doesn't work properly when I add this part:
    Code (CSharp):
    1.         if (Input.GetKeyDown(KeyCode.E) && boxpull.jointspring == 1)
    2.         {
    3.             boxpull.jointspring = 0;
    4.         }
    Can someone explain me why please?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    I have no idea what your script is supposed to be doing but I'd wager you're shooting yourself in the foot. On line ~22 there's this code which ~enables your spring when the "e" key is pressed:
    Code (CSharp):
    1.             if (Input.GetKeyDown(KeyCode.E))
    2.             {
    3.                 boxpull = box.GetComponent<BoxPull>();
    4.                 boxpull.jointspring = 1;
    5.             }
    Biut then right after that you have:
    Code (CSharp):
    1.        if (Input.GetKeyDown(KeyCode.E) && boxpull.jointspring == 1)
    2.         {
    3.             boxpull.jointspring = 0;
    4.         }
    This part is guaranteed to run if that first part ran in the same frame. So you will always immediately set boxpull.jointspring back to 0 on the same frame that you set it to 1.
     
    ProExCurator likes this.
  3. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    Yeah, you're right. I changed it to this:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         distance = Vector3.Distance(box.transform.position, transform.position);
    4.         if (distance <= minimumDistance)
    5.         {
    6.             if (Input.GetKeyDown(KeyCode.E))
    7.             {
    8.                 if (Epressed == false)
    9.                 {
    10.                     Epressed = true;
    11.                     boxpull = box.GetComponent<BoxPull>();
    12.                     boxpull.jointspring = 1;
    13.                 }
    14.                 else if (Epressed == true)
    15.                 {
    16.                     Epressed = false;
    17.                     boxpull = box.GetComponent<BoxPull>();
    18.                     boxpull.jointspring = 0;
    19.                 }
    20.             }
    21.         }
    22.     }
    And now it works like it should ;)