Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by DLEnfield · Nov 04, 2016 at 03:03 PM · c#scripting problem

Make event stop

So I have basic script for a mobile VR device which just about works, in that when Mouse0/Fire1 is activated, the Camera begins to move in whichever direction it is pointing. However, I want to be able to cancel this section of the script when the button is pressed again, i.e

Go from stationary to moving when Fire1 is pressed - Then moving to stationary when pressed again.

Any tips? Script below (excuse the mess, I'm pretty new to this so most of it's been sourced and modified)

 public Transform vrCamera;
 private Rigidbody cc;

 public float forwardSpeed = 50f;
 float rotationSpeed = 300f;

 Vector3 axis;
 float rotationY;
 float rotationX;
 float rotationZ;

 bool start = false;


 void Start () {
     cc = GetComponent<Rigidbody> ();

     vrCamera = Camera.main.transform;
 }


 void Update () {

     if (Input.GetButtonDown ("Fire1")) {
         

         start = true;
     }


     if(start == true)
     {

         FlightMode();
     }

 }

 void FlightMode(){            rotationX = vrCamera.transform.localRotation.x / 2;
                               rotationY = vrCamera.transform.localRotation.y / 2;
                               rotationZ = vrCamera.transform.localRotation.z;

     axis = new Vector3 (rotationX, rotationY, rotationZ);

     transform.Rotate (axis * Time.deltaTime * rotationSpeed);

     cc.velocity = vrCamera.transform.forward * forwardSpeed;
 }

}

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by frederik99 · Nov 04, 2016 at 03:56 PM

replace

 if (Input.GetButtonDown ("Fire1")) {
          
         start = true;
 }

with

 if (Input.GetButtonDown ("Fire1")) {
          
          start = !start;
 }

this will toggle the state everytime Fire1 is pressed.

Comment
Add comment · Show 6 · 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
avatar image DLEnfield · Nov 04, 2016 at 05:18 PM 0
Share

Thanks for the help - this does stop it to an extent, although rather than come to a complete stop, the camera just continues moving in the direction it was going regardless of where the camera is pointing. Anything else that might be tripping it up?

avatar image TreyH DLEnfield · Nov 04, 2016 at 06:17 PM 0
Share

Well yeah, you're setting velocity manually. This is typically a no-no for reasons like this, but you'll want to set that velocity to zero when start isn't true anymore.

avatar image DLEnfield TreyH · Nov 04, 2016 at 07:00 PM 0
Share

Yeah that makes sense. I've not really worked with velocity before to be honest, how would you embed that into the code? Thanks for the help

Show more comments
Show more comments
avatar image
0

Answer by ToasterKyle · Nov 04, 2016 at 07:18 PM

cc.velocity = start ? vrCamera.transform.forward * forwardSpeed : Vector3.zero;

This will reset the velocity to zero when start = false, but it looks like a few other things should also be reset when start = false so this might not be the best way

Comment
Add comment · Show 1 · 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
avatar image DLEnfield · Nov 04, 2016 at 07:47 PM 0
Share

Yeah I might have to rebuild the script, that snipped takes away the existing functionality :/ Thanks for the help though

EDIT: No it didn't, that was me being an idiot, the functionality's still there but it didn't stop the camera, might be because I don't have any clause that can set start = false but I really don't know at this point

EDIT 2: $$anonymous$$anaged to get it up and running, thanks for your help :)

avatar image
0

Answer by DLEnfield · Nov 04, 2016 at 08:18 PM

I seem to have managed to make it work by moving around snippets I've been given here and after a few shifts I got it right - still quite a messy code but I've taken out a lot of elements that weren't actually contributing and it's functional enough now. Thanks to everyone who contributed to this, honestly expected to just get berated for being inexperienced, means a lot that you'd help the way you did.

Comment
Add comment · Show 1 · 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
avatar image frederik99 · Nov 04, 2016 at 11:58 PM 0
Share

no problem mate. glad i could help.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to detect an object which be in FOV of certain camera ? 1 Answer

instantiating unique classes of a type 0 Answers

(C# and Java)Unable to give the Damage from enemy bullet script to Player Health. 3 Answers


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