Search Unity

can i look a value?

Discussion in 'Scripting' started by naorgueva, Jun 19, 2019.

  1. naorgueva

    naorgueva

    Joined:
    Apr 11, 2019
    Posts:
    25
    i have an object moving and a camera following him.

    i want when i press a key _B will "record" the position of the camera for the exact time i pressed the key
    and wont move until i press anther key

    var _B = transform.position;

    Code (CSharp):
    1. public void EagleState(New_Movement.eEagleState inComingState)
    2.     {
    3.  
    4.         _eagleState = inComingState;
    5.         switch (_eagleState)
    6.         {
    7.             case New_Movement.eEagleState.Non:
    8.                 _A = _bottomViewPos;
    9.                 _B = _topViewPos;
    10.                 break;
    11.  
    12.             case New_Movement.eEagleState.DiveDown:
    13.                 _A = _DiveViewPos;
    14.                 _B = transform.position;
    15.                 break;
    16.         }
    17.     }
     
    Last edited: Jun 19, 2019
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    There are different ways to accomplish this. Depending on how your objects are moving around, the simplest might be to set Time.timeScale to 0 when you press "B", and then set it to 1 again when you press the other key. Assuming you've wired up your motion to be time dependent (which is typical), everything should stop moving when you stop time like this.
     
  3. naorgueva

    naorgueva

    Joined:
    Apr 11, 2019
    Posts:
    25
    i dont want to stop time
    just to record the moment at wich you presed
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Well, you said you didn't want it to move... Did you just want that one object to stop moving? If so, then on key down, you'd get whatever component on the object is responsible for its motion, and call some method on it to make it stop moving.

    Anyway, getting transform.postion is exactly right is you want to know it's position. Do you already have Input.GetKeyDown() wired up to the keys you want to press?
     
  5. naorgueva

    naorgueva

    Joined:
    Apr 11, 2019
    Posts:
    25
    the problem is
    its in the: case New_Movement.eEagleState.DiveDown:
    so the GetKey dos dent register

    Code (CSharp):
    1. public void EagleState(New_Movement.eEagleState inComingState)
    2.     {
    3.         _eagleState = inComingState;
    4.         switch (_eagleState)
    5.         {
    6.             case New_Movement.eEagleState.Non:
    7.                 _A = _bottomViewPos;
    8.                 _B = _topViewPos;
    9.                 break;
    10.             case New_Movement.eEagleState.DiveDown:
    11.                 _A = _DiveViewPos;
    12.                   if (Input.GetKey(KeyCode.A))
    13.                 _B = transform.position;
    14.                 break;
    15.         }
    16.     }
     
  6. naorgueva

    naorgueva

    Joined:
    Apr 11, 2019
    Posts:
    25


    by the time it gets thare the GetKey dos dent register
     
  7. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    I wouldn't normally put a lot of logic in the 'case' of a switch like that, but what you have should generally work fine, assuming you're holding down "A" when the EagleState method is called. Understand that something else would need to have called EagleState; pressing "A" along isn't enough to get that code to run. Are you calling EagleState every frame, in Update or something?

    You can try adding logging to your method to see whether the right things are happening:

    Code (CSharp):
    1. public void EagleState(New_Movement.eEagleState inComingState)
    2.     {
    3.         Debug.Log($"EagleState was called with state {inComingState}.");
    4.         _eagleState = inComingState;
    5.         switch (_eagleState)
    6.         {
    7.             case New_Movement.eEagleState.Non:
    8.                 _A = _bottomViewPos;
    9.                 _B = _topViewPos;
    10.                 break;
    11.             case New_Movement.eEagleState.DiveDown:
    12.                 Debug.Log($"Is 'A' currently held? {Input.GetKey(KeyCode.A)}");
    13.                 _A = _DiveViewPos;
    14.                   if (Input.GetKey(KeyCode.A)) {
    15.                   Debug.Log("Okay, assigning position to _B");
    16.                 _B = transform.position;
    17.                 }
    18.                 break;
    19.         }
    20.     }
    The output should tell you what's being called and what isn't.