0
\$\begingroup\$

How can I clearly distinguish tapping and swiping touches?
This is my interaction script:

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;

/// /// <summary>
/// Calf and Finger Interaction Script
/// Raising likes, getting angry when you get upset, etc
/// /// </summary>

public class CowRelationManager : MonoBehaviour
{
    // implemented by dragging the event system
    //a better way than this
    //refactoring later if present
    [SerializeField]
    [Tooltip("variable to check how many times you poke")]
    private int CowClickCount = 0;

    [SerializeField]
    private float sweepThreshold = 50f; // Swipe threshold for touch detection
    [SerializeField]
    private float doubleTapTimeThreshold = 0.3f; // time threshold for detecting push with double touch

    TutorialSceneManager tutorialSceneManager;
    Animator CowAnimator;

    private void Start()
    {
        tutorialSceneManager = TutorialSceneManager.Instance;
        Debug.Log($"TutorialSceneManager is : {tutorialSceneManager.IsUnityNull()}");
        CowAnimator = tutorialSceneManager.cowScriptableObject.CowObject.GetComponent<Animator>();
        Debug.Log($"CowAnimator is : {CowAnimator.IsUnityNull()}");
    }


    private void Update()
    {
        // Touch input processing
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);

            Ray ray = Camera.main.ScreenPointToRay(touch.position);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.gameObject.name == "Cow")
                {
                    //Implementing rubbing
                    //on-the-of-touch
                    if (touch.phase == TouchPhase.Began)
                    {
                        Debug.Log("start cow stroke");
                    }
                    /* /* if (touch.phase == TouchPhase.Ended && touch.deltaTime > 0.05f)
                    {
                    //Touch over
                    Debug.Log ("Done rubbing");
                    Debug.Log("You've gained popularity.");
                    }*/
                    else if (touch.phase == TouchPhase.Moved && touch.deltaTime > 0.05f)
                    {
                        CowAnimator.SetTrigger("CowJump");
                        Debug.Log("stroke end");
                        Debug.Log("relationship is increase.");
                    }

                    //when you just poke it once
                    else if (touch.phase == TouchPhase.Ended && touch.deltaTime < 0.05f)
                    {
                        if (CowClickCount < 5)
                        {
                            Debug.Log("bulling the cow");
                            CowClickCount++;
                        }
                        else
                        {
                            CowAnimator.SetTrigger("CowAngry");
                            Debug.Log("cow get mad!!");
                            CowClickCount = 0;
                        }
                    }
                }
            }
        }
    }
}

I want to make two different interactions with my game objects - one from a touch & the other from a swipe (rub, stroke, etc). For example, swipe the pet object to increase relationship versus repeatedly tap a cow to make it mad.

But in the code, I can't distinguish between the two different interaction types; they always act together.

\$\endgroup\$
1
  • \$\begingroup\$ Please note the tag guidance for unityscript: you have a script in Unity written in C#; that's not the same thing as the very old, unityscript which was a Javascript like language. \$\endgroup\$
    – Pikalek
    Commented Jul 25, 2023 at 3:35

0

You must log in to answer this question.

Browse other questions tagged .