- Home /
Horizontal Slider
I'm trying to implement a slider in the Main Menu that adjusts the degree to which the camera shakes. The menu has a Play Game, Quit, and slider.
Here's my code for the slider:
using UnityEngine;
using System.Collections;
public class Shaky_Slider : MonoBehaviour {
public float hSliderValue = 0.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
hSliderValue = GUI.HorizontalSlider(new Rect(525, 980, 200, 30), hSliderValue, 0.0f, 10.0f);
if (hSliderValue == 0.0f)
{
iTween.ShakeRotation(gameObject,iTween.Hash("x", 0.0f,"y", 0.0f,"looptype",iTween.LoopType.none,"delay",0.0f,"time",0.0f));
}
if (hSliderValue > 0.0f && hSliderValue < 1.0f)
{
iTween.ShakeRotation(gameObject,iTween.Hash("x", 0.1f,"y", 0.05f,"looptype",iTween.LoopType.none,"delay",0.0f,"time",0.0f));
}
if (hSliderValue >= 1.0f && hSliderValue < 2.0f)
{
iTween.ShakeRotation(gameObject,iTween.Hash("x", 0.2f,"y", 0.1f,"looptype",iTween.LoopType.none,"delay",0.0f,"time",0.0f));
}
if (hSliderValue >= 2.0f && hSliderValue < 3.0f)
{
iTween.ShakeRotation(gameObject,iTween.Hash("x", 0.3f,"y", 0.3f,"looptype",iTween.LoopType.none,"delay",0.0f,"time",0.0f));
}
if (hSliderValue >= 3.0f && hSliderValue < 4.0f)
{
iTween.ShakeRotation(gameObject,iTween.Hash("x", 0.4f,"y", 0.5f,"looptype",iTween.LoopType.none,"delay",0.0f,"time",0.0f));
}
if (hSliderValue >= 4.0f && hSliderValue < 5.0f)
{
iTween.ShakeRotation(gameObject,iTween.Hash("x", 0.5f,"y", 0.9f,"looptype",iTween.LoopType.none,"delay",0.0f,"time",0.0f));
}
if (hSliderValue >= 5.0f && hSliderValue < 6.0f)
{
iTween.ShakeRotation(gameObject,iTween.Hash("x", 0.6f,"y", 1.3f,"looptype",iTween.LoopType.none,"delay",0.0f,"time",0.0f));
}
if (hSliderValue >= 6.0f && hSliderValue < 7.0f)
{
iTween.ShakeRotation(gameObject,iTween.Hash("x", 0.7f,"y", 1.7f,"looptype",iTween.LoopType.none,"delay",0.0f,"time",0.0f));
}
if (hSliderValue >= 7.0f && hSliderValue < 8.0f)
{
iTween.ShakeRotation(gameObject,iTween.Hash("x", 0.8f,"y", 2.1f,"looptype",iTween.LoopType.none,"delay",0.0f,"time",0.0f));
}
if (hSliderValue >= 8.0f && hSliderValue < 9.0f)
{
iTween.ShakeRotation(gameObject,iTween.Hash("x", 0.9f,"y", 2.5f,"looptype",iTween.LoopType.none,"delay",0.0f,"time",0.0f));
}
if (hSliderValue >= 9.0f && hSliderValue <= 10.0f)
{
iTween.ShakeRotation(gameObject,iTween.Hash("x", 1.0f,"y", 2.9f,"looptype",iTween.LoopType.none,"delay",0.0f,"time",0.0f));
}
}
}
When I adjust the bar in the menu, the camera changes its degree of shakiness accordingly. However, the problem is that the camera shakes in the menu scene. What do I have to do so that the camera shakes in the scene. Right now, I have the script on the Main Camera in the menu scene.
Any assistance is appreciated. Thank you for your time.
I should note that when I play game, it loads the scene that I want the camera to shake in. Again, thank you for your time.
Can you not just use different cameras for the menu and your main game scene, and disable and enable each one accordingly?
Also, why not just link your HorizontalSlider values directly to the x and y changes in the iTween.ShakeRotation call, as opposed to having the long chain of if statements that you have right now?
@robertbu You're right that I don't want it to shake in the menu but do want it to shake in the main scene. However, right now it's shaking in the menu and I want it to shake in the main scene. How do I do that?
@$$anonymous$$leptomaniac Regarding putting the HorrizontalSlider directly to the x and y changes: we just altered the code for the iTween.ShakeRotation so we can disregard that part. I saw your comment.I do have different cameras for the menu and the main game scene.
$$anonymous$$y camera in the main scene is called 'Camera' and it's tagged '$$anonymous$$ainCamera'
$$anonymous$$y camera in the menu is called '$$anonymous$$ainCamera' and it's tagged '$$anonymous$$ainCamera'
With this information, could you please help guide me as to how I would disable the '$$anonymous$$ainCamera' in the menu and enable the 'Camera' in the main scene.
I imagine I can put the code I need into the spot that says to load the main scene when the player clicks 'Play Game'
I know this is a lot of information. I appreciate any help @$$anonymous$$leptomaniac.
Answer by Kleptomaniac · Mar 09, 2013 at 11:45 PM
Ah, after reading your question a few more times, I think I've finally got it.
The reason that this is happening is because you have two cameras, and you are changing the ShakeRotation on the camera you don't want to change.
All you need to do is create an object reference to your scene camera: public GameObject sceneCam;
and then change your ShakeRotation calls so that instead of saying iTween.ShakeRotation(gameObject, etc.)
, you say iTween.ShakeRotation(sceneCam, etc.)
That should work. :)
Klep
Your answer
Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
Options Menu with volume sliders accessible from Main Menu scene and Game scene 2 Answers
I can not figure out The referenced script on this Behaviour (Game Object 'MainMenu') is missing 1 Answer
Multiplayer - Other players shaking back and fourth on moving platform 2 Answers
iTween - Increase shake over time? 1 Answer