Basic Touch Controls Added
Summary
I was able to figure out how to simulate and add the base touch controls for the game! I simply added the Terrier Tennis sprite as a place holder and was able to change the position of the Terrier based off my touch input. I will eventually want to replace the Terrier with a cat paw and some animation showing the paw travel across the screen to where the player last touched.
This was a lot more difficult than I was expecting and ended up following a fantastic tutorial by Samyam on how to properly implement touch controls. I also learned about Singleton classes and how to actually leverage {get; set;} for the first time! In order to figure this out effectively I leveraged a gist I found on github that provided the great details on the Singleton class. I also provided the link below for anyone that wants to just copy and paste.
using UnityEngine; // https://gist.github.com/mstevenson/4325117 public class MonoBehaviourSingleton<T> : MonoBehaviour where T : Component { private static T _instance; public static T Instance { get { if (_instance == null) { var objs = FindObjectsOfType (typeof(T)) as T[]; if (objs.Length > 0) _instance = objs[0]; if (objs.Length > 1) Debug.LogError("There is more than one " + typeof(T).Name + " in the scene."); if (_instance == null) { GameObject obj = new GameObject(); obj.hideFlags = HideFlags.HideAndDontSave; _instance = obj.AddComponent<T>(); } } return _instance; } } } public class SingletonPersistent<T> : MonoBehaviour where T : Component { public static T Instance { get; private set; } public virtual void Awake () { if (Instance == null) { Instance = this as T; DontDestroyOnLoad (this); } else { Destroy(gameObject); } } }
Using this Singleton and actually leveraging the Input System I feel I am truly using it in the way Unity intended instead of the "hacky" way I had in the past. I hope I can keep on this track of using this method as it seems to scale a lot better than what I had done in previous games.
Struggles
While I am able to get the simulated touch controls working great, I can't seem to get the touch controls to work on Unity Remote 5 on my phone. I did some digging and found out the reason is because I have not turned on developer mode on my phone, yet I do not seem to have the ability to or find any good resources online for how to turn this on. I believe I should just need to link my phone up to a mac and allow this functionality. In the meantime, I am comfortable that the touch controls will eventually work on the phone since I have no mouse inputs allowed yet. This shows the simulated touch is working and it is just my phone that is being the pain. I will want to figure this out asap.
Next Goal
Work on adding in the cat paw to follow the last touch position and add the yarn with physics so the player can try and keep it alive!