Search Unity

manipulating actions of a TouchPhase depending on a TouchPhase that happened before it?

Discussion in 'Scripting' started by MSachs, Feb 15, 2018.

  1. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122
    Hi,

    i have an orbit camera (freelook cinemachine) which is controlled by a Touch Input using TouchPhase.Moved. I also have different gameobjects in the scene with individual cameras which get blended to after tapping the gameobjects. For being able to control the camera and the blending to the other cameras I need to have this action on TouchPhase.Ended. My problem is that if I control the camera and release the touch whilst hovering over one of the gameobjects it blends to a different camera even though I only want to stop moving the camera.

    I hope this was somewhat understandable.

    My actual question:
    Is there a way to basically disable the TouchPhase.Ended action if TouchPhase.Moved happened immediately before it without lifting the finger?

    Thanks in advance :)
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Sure, set a bool. If your touchphase is Moved, set a bool to true. Then when Ended is checked, check the bool, if it's true, don't do anything except set the bool back to false. If its false, then run your ended code as normal.
     
    MSachs likes this.
  3. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122

    Thanks that sounds like a good idea! I am still rather new to scripting and everything I tried today didn't work... Would you mind to help out a little?

    I currently have two scripts. One which controls the camera movement and one which controls the blending between cameras. I am mostly struggling with accessing the bool from inside the second script

    movement:
    Code (CSharp):
    1. using Cinemachine;
    2. using UnityEngine;
    3.  
    4. [RequireComponent(typeof(CinemachineFreeLook))]
    5. public class CamMoveOnClick : MonoBehaviour
    6. {
    7.     public bool _freeLookActive;
    8.  
    9.     // Use this for initialization
    10.     private void Start ()
    11.     {
    12.         CinemachineCore.GetInputAxis = GetInputAxis;
    13.     }
    14.  
    15.     private void Update()
    16.     {
    17.         if(Input.touchCount == 1)
    18.         {
    19.         if (Input.GetTouch(0).phase == TouchPhase.Moved)
    20.             {
    21.                 _freeLookActive = true;
    22.             }
    23.  
    24.             else
    25.             {
    26.                 _freeLookActive = false;
    27.             }
    28.         }
    29.     }
    30.  
    31.     private float GetInputAxis(string axisName)
    32.     {
    33.         return !_freeLookActive ? 0 : Input.GetAxis(axisName == "Mouse Y" ? "Mouse Y" : "Mouse X");
    34.     }
    35. }

    camera blending
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6. public class ChangeCamAndActiveYellow : MonoBehaviour
    7. {
    8.  
    9.     public GameObject yellow;  
    10.     public GameObject orange;
    11.     public GameObject green;
    12.     public GameObject blue;
    13.  
    14.     public CinemachineFreeLook freeLookYellow;
    15.     public CinemachineFreeLook freeLookOrange;
    16.     public CinemachineFreeLook freeLookGreen;
    17.     public CinemachineFreeLook freeLookBlue;
    18.     public CinemachineFreeLook freeLookAll;
    19.  
    20.     public CinemachineFreeLook freeLookGeraet1;
    21.     public CinemachineFreeLook freeLookGeraet2;
    22.     public CinemachineFreeLook freeLookGeraet3;
    23.  
    24.     bool mouseOver;
    25.  
    26.  
    27.     void Start ()
    28.     {
    29.         freeLookYellow.gameObject.SetActive (false);
    30.         freeLookOrange.gameObject.SetActive (false);
    31.         freeLookGreen.gameObject.SetActive (false);
    32.         freeLookBlue.gameObject.SetActive (false);
    33.         freeLookAll.gameObject.SetActive (true);
    34.  
    35.         freeLookGeraet1.gameObject.SetActive (false);
    36.         freeLookGeraet2.gameObject.SetActive (false);
    37.         freeLookGeraet3.gameObject.SetActive (false);
    38.  
    39.         yellow.SetActive (true);
    40.         orange.SetActive (true);
    41.         green.SetActive (true);
    42.         green.SetActive (true);
    43.     }
    44.  
    45.     void Update ()
    46.     {      
    47.         if (Input.touchCount == 1)
    48.         {
    49.             if (mouseOver && Input.GetTouch (0).phase == TouchPhase.Ended)
    50.             {
    51.                 freeLookYellow.gameObject.SetActive (true);
    52.                 freeLookOrange.gameObject.SetActive (false);
    53.                 freeLookGreen.gameObject.SetActive (false);
    54.                 freeLookBlue.gameObject.SetActive (false);
    55.                 freeLookAll.gameObject.SetActive (false);
    56.  
    57.                 freeLookGeraet1.gameObject.SetActive (false);
    58.                 freeLookGeraet2.gameObject.SetActive (false);
    59.                 freeLookGeraet3.gameObject.SetActive (false);
    60.  
    61.                 yellow.SetActive (true);
    62.                 orange.SetActive (false);
    63.                 green.SetActive (false);
    64.                 blue.SetActive (false);
    65.             }
    66.         }
    67.     }
    68.  
    69.  
    70.     void OnMouseOver ()
    71.     {
    72.         mouseOver = true;
    73.     }
    74.  
    75.     void OnMouseExit ()
    76.     {
    77.         mouseOver = false;
    78.     }
    79. }
    80.  
    Sorry if the codes look a little messy... Like I said I am pretty new to all this ;-)
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Since it sounds like you have both scripts in the scene right now, just make a public variable on the second script of type CamMoveOnClick and then drag and drop the other script onto the variable from the scene. Then you can access it in the second script through that variable and check the value of your bool.

    So...
    second script has

    Code (CSharp):
    1. public CamMoveOnClick camMove;
    then you connect the script in the inspector.
    then just check it with

    Code (CSharp):
    1. if(!camMove._freeLookActive)
     
    MSachs likes this.
  5. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122
    Thanks! will try that right away! :)
     
  6. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122
    it works most of the time but the problem is that it seems to register every ever so slight movement of the finger as TouchPhase.Moved so if I want to just tap the gameobject to blend to the other camera it sometimes doesn't work. Do you know if there is a way to define basically a distance which the finger can move so it doesn't count as TouchPhase.Moved? :)
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    You'll need to define a distance yourself.
    https://docs.unity3d.com/ScriptReference/TouchPhase.html

    You should be able to get an idea from Unity's example. Set a start position. Then compute distance and compare it to a predetermined value. Moved should trigger each time it's moved. Just always check against the start position till it's far enough.
     
    MSachs likes this.