- Home /
Do I need a helper class?
So I don't know what a helper class is or at least I don't know how to use one in Unity3D.
I'm trying to get TouchInputHelper.cs
to clean up my code. I will be using it to handle all the things I might need to do with touches. I don't want to be rewriting code in different classes so I thought I could just put my needed functions into my TouchInputHelper.cs
and start using that in other classes. I don't really know the term for what I'm trying to do.
I have a ShipRotation.cs
and a DragMove.cs
which both use a method I wrote that will take a Victor2 and convert it into a world point. Right now it's just test code to get these two classes talking to TouchInputHelper.cs
but it doesn't seem to be working and I don't know what I'm doing wrong.
public class ShipRotation : MonoBehaviour
{
ShipControls _shipControls;
public GameObject ship;
TouchInputHelper touchInputHelper;//Adding it here so I can use this classes functions
// Use this for initialization
void Start()
{
_shipControls = GetComponent<ShipControls>();
}
RaycastHit hit;
public LayerMask touchInputMask;
Vector3 target;
void Update()
{
//ship.transform.Rotate(Vector3.right * Time.deltaTime);
if (Input.touchCount > 0)
{
foreach (Touch touch in Input.touches)
{
Ray ray = Camera.main.ScreenPointToRay(touch.position);
if (Physics.Raycast(ray, out hit, touchInputMask))
{
GameObject recipient = hit.transform.gameObject;
if (recipient.tag == "Player")
{
//I'm not sure if this line is working
target = touchInputHelper.turnTouchInputToWorldPoint(new Vector2(touch.position.x, touch.position.y));
}
}
}
ship.transform.LookAt(target);
}
public float damping = 10f;
}
Here is what I'm trying to use as a helper class. It is only written with test code right now.
using UnityEngine;
using System.Collections;
public class TouchInputHelper : MonoBehaviour{
bool usingTouch = true;
Vector3 gameObjectToScreenPoint;
public string hello= "test";
public Vector3 turnTouchInputToWorldPoint(Vector2 v2)
{
Vector3 v3;
if (usingTouch)
{
v3 = new Vector3(v2.x, v2.y, gameObjectToScreenPoint.z);
}
else
{
v3 = new Vector3(1, 1, 10);
//v3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, gameObjectToScreenPoint.z);
}
return v3;
}
}
What am I doing wrong? Do I need to add TouchInputHelper.cs
as a component then use GameObject.Find
to use it?
What are you trying to do? I'm not even sure what this code is supposed to do.
I'm trying to get TouchInputHelper.cs
to clean up my code. I will be using it to handle all the things I might need to do with touches. I don't want to be rewriting code in different classes so I thought I could just put my needed functions into my TouchInputHelper.cs
and start using that in other classes. I don't really know the term for what I'm trying to do.
Answer by siaran · Jun 01, 2015 at 06:27 PM
It may be a good idea to make your TouchInputHelper class static (and not a MonoBehaviour).
I don't really see anything /wrong/ with your code though, and it's not exactly clear what your problem is...
It doesn't seem like line 32 in ShipRotation.cs
is running at all.
Would making it a static object fix this? Can you give me some sample code?
No, making it a static object won't magically get a function on it called when it isn't.
If that line isn't called for some reason, then something in your conditions is wrong. Could be many things in your code... (wrong tag? wrong layermask?
(Is there any reason you are not using unity's built-in Camera.ScreenToWorldPoint method?).
In this case no. $$anonymous$$y question is more on how do I get this helper class (Is this the right term?) to work. Right now it just looks like I'm using this class to use the built in method Camera.ScreenToWorldPoint
but I want to be adding more things to it later that I will want to share with other classes.
I've checked the tag and layermask that doesn't seem to be an issue.
Answer by POiD · Jun 01, 2015 at 09:35 PM
Trying to centralize some functions can be done various ways depending on your needs.
Either you need to create an object and then use the member functions on that object, or you create a static class and call static functions.
If using the first method, you'll need to either use the new() keyword to create the object, or if you are adding the script to an object in Unity you can try and get a copy of it via GameObject.FindObjectOfType<>(). This works in combination with the singleton pattern, if you only want 1 instance of the object to exist.
If you don't need an object, you could use the 2nd option and create a "public static class" that then has helper functions of type "public static" in it.
It really comes down to are you needing to start variables between calls to the function (object is better) or are all inputs to the function provided in the call, and thus you don't need to store variables in the "object".