- Home /
Enable bool while interacting with a slider
The question might not make much sense, I honestly have little experience with scripting so I just want to spell out what I'm trying to do in case there's a better solution. I'm making a mobile space shooter for a school project. The ship moves automatically forward and the player can regulate the ship's speed with a slider and also be able to rotate the ship with another slider. Finally the player can touch and drag the ship left and right and ship will fire projectiles while touching it. I've gotten the basics layed out in the script using transform.forward to automatically move it, a public function that a UI slider uses to regulate the speed and finally I have a slider that rotates the ship. However, I've noticed while testing that having the ship move while you're rotating can lead to less control and it's difficult to navigate precicely. My solution was to maybe have a bool called "turning" that while active would stop the movement of the ship, but I quickly realized that if I put turning = true;
inside the function that rotates the ship I will have no way of re-enabling the movement. I thought of coroutines, but that can also be troublesome. This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class transformForward : MonoBehaviour
{
[SerializeField]
float speed = 10;
public Slider compass;
float previousRotation;
bool firing = false;
bool turning = false;
private void Awake()
{
this.compass.onValueChanged.AddListener(this.ChangeRotation);
this.previousRotation = this.compass.value;
}
private void Update()
{
if (firing == true && turning == false)
{
transform.position += transform.up * (speed/2) * Time.deltaTime;
}
else if (turning == false && firing == false)
{
transform.position += transform.up * speed * Time.deltaTime;
}
//Haven't put in a shooting mechanic yet, but just wanted to test if the speed is reduced while firing
if (Input.GetKey(KeyCode.Space))
{
firing = true;
}
else
{
firing = false;
}
}
public void adjustSpeed(float newSpeed)
{
speed = newSpeed;
}
void ChangeRotation(float value)
{
float delta = value - this.previousRotation;
this.transform.Rotate(Vector3.forward * delta * -360);
this.previousRotation = value;
}
}
Any idea to how I can pause the movement while rotating?
Answer by Ermiq · Mar 21, 2021 at 09:20 AM
You could set turning = true
in the ChangeRotation()
method and reset to false
in the end of Update()
.
So, if the compass slider value is not getting changed in the current frame the turning
remains false
. And when the slider's onValueChanged
is triggered, the turning
becomes true
during the frame until it get reset back to false
in the end of Update()
.
Your answer
Follow this Question
Related Questions
Finish rotation in the same angle of beginning? 1 Answer
Player not facing the mouse correctly 1 Answer
GameObject facing fowards 1 Answer
Resetting transform's original position and rotation after using transform.rotateAround 1 Answer
!Urgent! How to get a sprite to look at another object? 1 Answer