- Home /
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;
}
}
}
I formatted it for you, but please use the code format button from here on.
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;
}
}
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
Follow this Question
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