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 JohnnyCenter · Mar 20, 2021 at 09:10 PM · rotationtransformbooleanslider

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?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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().

Comment
Add comment · Share
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

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

180 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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