Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Funlamb · Jun 01, 2015 at 06:20 PM · c#

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image valyard · Jun 01, 2015 at 06:26 PM 0
Share

What are you trying to do? I'm not even sure what this code is supposed to do.

avatar image Funlamb · Jun 01, 2015 at 06:55 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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...

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Funlamb · Jun 01, 2015 at 06:58 PM 0
Share

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?

avatar image siaran · Jun 01, 2015 at 09:25 PM 0
Share

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?).

avatar image Funlamb · Jun 01, 2015 at 10:49 PM 0
Share

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.

avatar image
0

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".

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges