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 bojan1986819 · Apr 12, 2015 at 05:58 PM · c#controllerballcontrol

What is wrong with my control switch script c#

I created a script which has a float in it called controlType. If it is set 1 then arrow keys and/or touch joystick should my ball. If it is set to 2 then the accelerometer should work. Also the jostick is deisappears for controlType2 and reappears when 1. It is set to 1 at start and it works correctly. Then during play if I change it to 2 joystick disappears and acceleromoter works correctly. The problem starts when I set it back to 1. The joystick reappears but it doesnt move my ball anymore and I have absolute no clue why. Here is the code below:

      public float speed;
     public float torque;
 
     public float ice;
 
     public float tiltSpeed;
 
     public float controlType=1;
 
     public GameObject joystick;
 
     void Start () {
         joyControlType ();
 
     }
 
 
         if (controlType == 1) {
             // touch joystick
             joystick.SetActive(true);
             float moveHorizontal = CrossPlatformInputManager.GetAxis ("Horizontal");
             float moveVertical = CrossPlatformInputManager.GetAxis ("Vertical");
             
             
             Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
             
             GetComponent<Rigidbody> ().AddForce (movement * speed * Time.deltaTime);
             GetComponent<Rigidbody> ().AddTorque (Vector3.up * torque * moveHorizontal);
             GetComponent<Rigidbody> ().AddTorque (Vector3.up * torque * moveVertical);
 
             // keyboard
 
             float moveHorizontalk = Input.GetAxis ("Horizontal");
             float moveVerticalk = Input.GetAxis ("Vertical");
         
         
             Vector3 movement3 = new Vector3 (moveHorizontalk, 0.0f, moveVerticalk);
         
             GetComponent<Rigidbody> ().AddForce (movement3 * speed * Time.deltaTime);
             GetComponent<Rigidbody> ().AddTorque (Vector3.up * torque * moveHorizontalk);
             GetComponent<Rigidbody> ().AddTorque (Vector3.up * torque * moveVerticalk);
         }
         if (controlType == 2) {
             // accelerometer
             joystick.SetActive(false);
             // Building of force vector 
                     Vector3 movement2 = new Vector3 (Input.acceleration.x, 0.0f, Input.acceleration.y);
             // Adding force to rigidbody
                     GetComponent<Rigidbody> ().AddForce(movement2 * tiltSpeed * Time.deltaTime);
                     GetComponent<Rigidbody> ().AddTorque (Vector3.up * torque * Input.acceleration.x);
                     GetComponent<Rigidbody> ().AddTorque (Vector3.up * torque * Input.acceleration.y);
         }
     }


I hope someone can find the solution.

Comment
Add comment · Show 5
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 bojan1986819 · Apr 12, 2015 at 06:02 PM 0
Share

hmm I just found one clue. If I take out the part which deactivates/reactivates the joystick then it works correctly even after I switch back to 1. The only problem is that it doesnt look to good to have a not working joy on the screen when acceleromater is being used.

avatar image equus_ligneus · Apr 12, 2015 at 06:12 PM 0
Share

Why do you use a float for control switch? using an int or an enum is more precise (less errors due to float not being exactly 2) and propably faster. Or do you blend between control types? As for your joystick, disable the renderer on the joystick when activating accelerometer, enable when enabling the joystick.

avatar image bojan1986819 · Apr 12, 2015 at 06:27 PM 0
Share

float is good for me and it is linked to a menu too and works correctly. I think the problem is due to my Joystick. It comes from standard assets. The canvas is in a children object which cannot be switched off. But it has an Image(script) on it and I tried to inactivate/reactivate it manually and it worked. The only problem is that I have no idea how to turn off only the Image of a GameObject in a code. :(

avatar image equus_ligneus · Apr 12, 2015 at 06:38 PM 1
Share

Well the Image class is in namespace UnityEngine.UI. Add

 using UnityEngine.UI;

at the top of your script, create a public member of type Image in your script and drag the image into the slot in the inspector. Set the enabled field on the image to true or false, depending on whether you want it to be shown or not.

avatar image bojan1986819 · Apr 12, 2015 at 06:45 PM 0
Share

haha it seems like answers are usually easier then we think :D thank you very much now everything works perfectly

0 Replies

· Add your reply
  • Sort: 

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

19 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

Related Questions

How to make camera position relative to a specific target. 1 Answer

i need to control ball like in impossible road game 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Move ball inside pipe like tunel 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