Search Unity

Sliders to control my Spaceships Engine Thrust and heading

Discussion in 'Getting Started' started by DerrickMoore, Jan 23, 2020.

  1. DerrickMoore

    DerrickMoore

    Joined:
    Feb 4, 2018
    Posts:
    246
    so.. I have a spaceship... and I just figured out how to have a button on screen that with a mouseclick will launch a "missile" or a "lander" gameobject.. from a big, city sized first rate Spaceship.. with a small flotilla of "pilot ships" around it. during a war..

    Well, I've got a button on screen, and I've placed some sliders on the same canvas panal.. and I THINK it's the only Canvas to have raycasting on..

    I didn't see any tutorials on that or any recent answers on a goggle search, so I thought i'd ask here..

    Frankly, looking at the Documentation on Sliders was a little overwhelming to me.. (despite 2 years of using Unity.. with a lot of help from this forum).. I really didn't understand the terms or where to start.



    now.. what I think I need to do is.. I have a script that runs my spaceships engines.. right now it's keyed to the W key for forward thrust.. but what I need to do is add the code to my engine script to point to the slider thing.. it came to me how to do it in the middle of the night...

    so, right now these "launch missile" and "UpSlider" are in the "main menu" script, casue I'm just learning.. but, I'll (I'll remember to add the "using UnityEngine.UI;" stuff at the top of the engine script.. (and "missiles" will go on another script, one that lives on the "missile launcher"

    I'm no coder, but I'm learning, and I really appreciate all the advice I get here


    Code (CSharp):
    1.     public void SubmitSliderSetting()
    2.     {
    3.         //Displays the value of the slider in the console.
    4.         Debug.Log(UpSlider.value);
    5.     }
    6.  
    7.     public void INFO()
    8.     {
    9.         SceneManager.LoadScene("Story00");
    10.     }
    11.  
    12.     public void NewGame()
    13.     {
    14.         SceneManager.LoadScene(newGameScene);
    15.     }
    16.  
    17.     public void PlayWarp()
    18.     {
    19.         //SceneManager.LoadScene("theWarp");
    20.         SceneManager.LoadScene("RandomStateofGalaxy");
    21.         // additive scene loading... so brings Galaxy Map into the world
    22.         //SceneManager.LoadScene("044GalaxyMap", LoadSceneMode.Additive);
    23.  
    24.     }
    25.  
    26.     public void MISSILE()
    27.     {
    28.         Instantiate(GameMissile, transform.position + (transform.forward * distanceFromPlayer), Quaternion.identity);
    29.         NMissiles = NMissiles - 1;
    30.     }
    31.  

    here we go with a link, just a "proof of concept" and a step towards making my "dream game"

    https://derrickmoore.itch.io/highborn
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Is the value of the slider something you're going to use every frame or FixedUpdate? Or is it something you just need whenever the slider changes? If I'm imagining it correctly, if the value of the slider is greater than 0 you're wanting to apply thrust, either with physics or by adjusting the transform.position. So you can just multiply the thrust force by the slider value. You configure the slider so it produces sane values to be multiplied by thrust.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yes, so there are basically two ways to use the slider. Don't worry, both of them are pretty easy. :)
    1. In your engine script, add a public Slider engineSlider; property (remembering to include UnityEngine.UI at the top of your script). In the Inspector of your ship, drag the slider in to this slot. Then in Update or whatever, simply use engineSlider.value as your current engine thrust level. OR,
    2. In your engine script, have a public method like public SetThrust(float thrust). Don't worry about including UnityEngine.UI at the top of your script in this case. In the Inspector of your slider, click the + button on the ValueChanged event, drag in your ship, and have it invoke the SetThrust method, using the "dynamic" version at the top of the menu. This takes the value from the slider, and passes it directly to your SetThrust method. Your SetThrust method should then do whatever it needs to do with the given thrust level.
    Both methods are fine; use whichever one makes more sense to you.
     
    DerrickMoore and Joe-Censored like this.
  4. DerrickMoore

    DerrickMoore

    Joined:
    Feb 4, 2018
    Posts:
    246
    Every Frame, with physics, I beleave.. because if the slider is set to a low value, I want a slow acceleration, fast acceleration for a high value.. 0-2.. something like that..
     
  5. DerrickMoore

    DerrickMoore

    Joined:
    Feb 4, 2018
    Posts:
    246
    thanks, I get messed up by silly things.. like ranges,. "first number is exclusive, second number is inclusive"

    its coming along, and being able to put controls on the screen really makes a difference.. feels less "fiddly" than having to relay solely on key commands

    work in progress, (took a few days to get back here, because.. I think things work better the second time around, so I spent a couple days re-making the mesh models for the engines.. seemed like a good idea to start from scratch...
    https://derrickmoore.itch.io/highborn
     
    JoeStrout likes this.
  6. DerrickMoore

    DerrickMoore

    Joined:
    Feb 4, 2018
    Posts:
    246
    I've got some kinda issue with my Canvas.. so, I gotta study up on exactly what I'm doing with the Canvas..

    I've been told that I should not have, like, 7 Canvases.. which I do.. a different Canvas for each Camera, and a camera for each camera mode and view and well there are ALOT of cameras in the scene. (I've been consolidating them.. what's left is just stuff like the "F5 Camera On" text on screen to show that the F5 Camera is active.. which is probably as inefficient as I could make it)

    but I have made a new engine script, and it's almost ready to go.. I just can't start on the slider now until I can get the canvas to remain active consistently. (I bet it's something stupid I missed, i'm checking through Justin Weinberger's Udemy course now, see if it was in there)

    learning about the Canvas.. I think this issue cause I have an image by my slider and buttons and the image keeps flickering..
    https://docs.unity3d.com/ScriptReference/Canvas.ForceUpdateCanvases.html

    I couldn't figure out how to add an image, so I put the images on here.
    https://derrickmoore.itch.io/testing-highborn