Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 manishsaini74 · Jun 13, 2018 at 12:55 PM · scripting problemphysicscharactercontrollerbutton trigger events

using a button to stop running script

Hi,

I have a cube which is moving on x and z axis i want to make a release button by which cube will stop moving and fall down to ground using gravity.

My code for movement is :

 using UnityEngine;
 using System.Collections;
 
 public class BlockMover : MonoBehaviour
 {
     // define the possible states through an enumeration
     public enum motionDirections { Spin, Horizontal, Vertical };
 
     // store the state
     public motionDirections motionState = motionDirections.Horizontal;
 
     // motion parameters
     public float spinSpeed = 180.0f;
     public float motionMagnitude = 0.05f;
 
     // Update is called once per frame
     private void Update()
     {
 
         // do the appropriate motion based on the motionState
         switch (motionState)
         {
             case motionDirections.Spin:
             // rotate around the up axix of the gameObject
             gameObject.transform.Rotate(Vector3.up * spinSpeed * Time.deltaTime);
             break;
 
             case motionDirections.Vertical:
             // move up and down over time
             gameObject.transform.Translate(Vector3.up * Mathf.Cos(Time.timeSinceLevelLoad) * motionMagnitude);
             break;
 
             case motionDirections.Horizontal:
             // move up and down over time
             gameObject.transform.Translate(Vector3.right * Mathf.Cos(Time.timeSinceLevelLoad) * motionMagnitude);
             break;
         }
     }
 }
Comment
Add comment · Show 2
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 bakir-omarov · Jun 13, 2018 at 01:06 PM 0
Share

Share code using CODE button, so we can read it and help you. alt text

2018-06-13-17-05-29.jpg (19.3 kB)
avatar image TreyH · Jun 13, 2018 at 01:09 PM 0
Share

I formatted it for you, but please use the code format button from here on.

1 Reply

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

Answer by bakir-omarov · Jun 13, 2018 at 01:37 PM

Make sure you have Rigidbody on you GameObject. And then you can call DisableMoving() method for disabling movement and falling down. You can attach it to OnButtonClick in Unity-UI Button.


 public class BlockMover : MonoBehaviour
 {
     private bool m_disableMoving;
     private Rigidbody rb;
 
     private void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     // define the possible states through an enumeration
     public enum motionDirections { Spin, Horizontal, Vertical };
 
     // store the state
     public motionDirections motionState = motionDirections.Horizontal;
 
     // motion parameters
     public float spinSpeed = 180.0f;
     public float motionMagnitude = 0.05f;
 
     // Update is called once per frame
     private void Update()
     {
         if (!m_disableMoving)
         {
             // do the appropriate motion based on the motionState
             switch (motionState)
             {
                 case motionDirections.Spin:
                     // rotate around the up axix of the gameObject
                     gameObject.transform.Rotate(Vector3.up * spinSpeed * Time.deltaTime);
                     break;
 
                 case motionDirections.Vertical:
                     // move up and down over time
                     gameObject.transform.Translate(Vector3.up * Mathf.Cos(Time.timeSinceLevelLoad) * motionMagnitude);
                     break;
 
                 case motionDirections.Horizontal:
                     // move up and down over time
                     gameObject.transform.Translate(Vector3.right * Mathf.Cos(Time.timeSinceLevelLoad) * motionMagnitude);
                     break;
             }
         }
     }
 
     public void DisableMoving()
     {
         m_disableMoving = true; // activating bool, so movement will stop
         rb.isKinematic = false; // disable if it is kinematic, so it can now affect like dynamic physic body
         rb.useGravity = true; // we are using gravity, so object can drop to the ground
 
         // uncomment this if you want to freeze object in his position and drop to the gorund vertically
         //rb.velocity = Vector3.zero;
     }
 }



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 manishsaini74 · Jun 14, 2018 at 07:46 AM 0
Share

hI bakir-omarov , This code can stop my object from moving but i am unable to change gravity to true and kinematic to false.

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

210 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 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

How do I solve this error? 2 Answers

Script adds rigidbody but player can walk straight through the gameobject? 1 Answer

Move CharacterController a specified distance away from starting position. 1 Answer

Need help with Raycasting 1 Answer

Gravity still being applied with unity CharacterController 0 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