- Home /
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.
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.
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.
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. :(
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.
haha it seems like answers are usually easier then we think :D thank you very much now everything works perfectly
Your answer

Follow this Question
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